Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-2008, 08:23 AM
bodhi2016 bodhi2016 is offline
Newbie
 
Join Date: Jan 2008
Posts: 29
Rep Power: 4
bodhi2016 is on a distinguished road
Default Problem with the server software

I have made the a portion of the server software(i did it till the bind portion) .
Code:
#include<windows.h>
#include<iostream.h>
int main()
{
    WSAData wsadata;
    if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0);
       {cout<<"winsock startup  failed\n"<<WSAGetLastError();
       WSACleanup();
       return -1;
       }
     cout<<"winsock startup is succes\n";
     SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
     if(servsock ==INVALID_SOCKET)
     {cout<<"socket init failed\n";
     return -1;
     }
    cout<<"socket init\n"; 
    sockaddr_in sin;
    sin.sin_port=htons(80);
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_family=AF_INET;
    if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
    {cout<<"FAILED TO BIND"<<WSAGetLastError();
    WSACleanup();
    return -1;
    }
    std::cout<<"Bind successful!\n";
    WSACleanup();
    return 0;
}
I usually compile my programme from time to time .When i compiled the above code i got many errors but i was able to tackle them but few error i cant do away with are these one:-
--------------------------------------------------------------------------
32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h multiple definition of `main'
32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h first defined here
32:2 D:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h ld returned 1 exit status
--------------------------------------------------------------------------
Void or methodz can you please tell me where i am going wrong
Thank You
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-20-2008, 09:05 AM
MeTh0Dz MeTh0Dz is offline
SLICE OWNZ YOUR SOUL
 
Join Date: May 2008
Posts: 294
Last Blog:
Ternary Operator CPP
Rep Power: 0
MeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura about
Default Re: Problem with the server software

First of all you are using <iostream.h>, which is a deprecated header. You just need to use <iostream>.

Next, you can't use 'cout' without 'using namespace std;', because 'cout' is in the std namespace. That is unless you do this 'std::cout', at every instance of 'cout'.

There is also no reason to 'return -1' in your main function, it doesn't matter what you return because you are in the main function and it is going to exit your program regardless.

The way you were doing WSAStartup was not working for me, and I looked at it for a while and it looked alright, so I just thought I'd show you this alternative method which I know works for sure.

Code:
int ret;
    WSAData wsadata;
    WORD ver = MAKEWORD(2, 0);
    ret = WSAStartup(ver,&wsadata);
    if (thing !=0) 
       {cout<<"winsock startup  failed\n"<<WSAGetLastError();
       getch();
       WSACleanup();
       return -1;
       }
Do that stuff and then tell me how it works.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-21-2008, 04:47 PM
bodhi2016 bodhi2016 is offline
Newbie
 
Join Date: Jan 2008
Posts: 29
Rep Power: 4
bodhi2016 is on a distinguished road
Default Re: Problem with the server software

Methodz i know that the standard header file of a c++
programme is
#include<stdio>
not
#include<stdio.h>
but it works
similarly we can use cout without
using namespace std.I know its a bad habit.But it works
I figured out the problem ,i was keeping
both win_connect and win_listen in same source file,So
i transferred win_listen to another project (i did not change the
code)and it was working pefectly(i have attached a screenshotof the output take a look),
then i updated the programme :-
Code:
#include<windows.h>
#include<iostream.h>
int main()
{
    WSAData wsadata;
    if (WSAStartup(MAKEWORD(2,0),&wsadata)!=0)
       {cout<<"winsock startup  failed\n"<<WSAGetLastError();
       WSACleanup();
       return -1;
       }
     cout<<"winsock startup is succes\n";
     SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
     if(servsock ==INVALID_SOCKET)
     {cout<<"socket init failed\n";
     return -1;
     }
    cout<<"socket init\n"; 
    sockaddr_in sin;
    sin.sin_port=htons(80);
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_family=AF_INET;
    if (bind(servsock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
    {cout<<"FAILED TO BIND"<<WSAGetLastError();
    WSACleanup();
    return -1;
    }
    cout<<"Bind successful!\n";
    WSACleanup();
    while (listen(servsock,SOMAXCONN)==SOCKET_ERROR)
    SOCKET client;
    int len = sizeof(sin);
    client=accept( servsock,(sockaddr*)&sin,&len);
    cout<<"Connection established!\n";
    closesocket(client);
    closesocket(servsock);
     WSACleanup();
   return 0;
}
I cannot understand the purpose of creating another socket client.
Please can you tell me the difference between listen and accept.
Shouln't the servsocket be listening as well as accept incoming connections.
And an error that i am getting is :-
34 D:\Dev-Cpp\Win_Listen.cpp `client' undeclared (first use this function)
Can anybody tell me why i am receiving this error i have declared
"client"
Thank You
Attached Files
File Type: rar winsock.rar (36.7 KB, 0 views)

Last edited by WingedPanther; 06-23-2008 at 12:05 PM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-22-2008, 09:44 PM
MeTh0Dz MeTh0Dz is offline
SLICE OWNZ YOUR SOUL
 
Join Date: May 2008
Posts: 294
Last Blog:
Ternary Operator CPP
Rep Power: 0
MeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura about
Default Re: Problem with the server software

You have to put the server into listen mode, before it can accept connections.

What are you talking about? You can't use cout without using namespace std; or std::cout.

The point is not whether <iostream.h> will work, it is the fact that it is a deprecated header, if you don't understand that then maybe you should look it up.

You create the other socket, because it might be a server that has received connections from multiple hosts. In that case you want to only send the appropriate data for each host, to the correct host. Creating client sockets allows you to do this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-23-2008, 01:37 AM
TkTech TkTech is offline
 
Join Date: Jun 2006
Posts: 1,034
Last Blog:
Having trouble with yo...
Rep Power: 20
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default Re: Problem with the server software

Quote:
What are you talking about? You can't use cout without using namespace std; or std::cout.
Some of the older 'simple' IDE's (codewarrior for example) automaticly include STD. Some new users may not be familiar with namespaces because of this.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 06-23-2008, 02:37 PM
bodhi2016 bodhi2016 is offline
Newbie
 
Join Date: Jan 2008
Posts: 29
Rep Power: 4
bodhi2016 is on a distinguished road
Default Re: Problem with the server software

Methodz i have changed the code as you said by correcting that cout to std::cout.My client software is Win-Connect which is a in the project winsock.The code that i have written is :-

Code:
#include<windows.h>;
#include<iostream>;
int main()
{
    WSAData wsadata;
    if(WSAStartup(MAKEWORD(2,0),&amp;wsadata)!=0)
    {
    std::cout<<"1:)startup failed\n"<<WSAGetLastError();
    WSACleanup();
    return -1;
    }
  else
    {
     std::cout<<"2:)Socket Init Success\n"<<WSAGetLastError();
     }
     SOCKET mysock=socket(AF_INET,SOCK_STREAM,0);  
     if(mysock==INVALID_SOCKET )
     {
      std::cout<<"3:)Socket Init Failed\n";
      WSACleanup();
      return -1;
     } 
     
     else
     {
         std::cout<<"4:)Socket Init Success\n";                                                                                                        
         sockaddr_in sin;
         sin.sin_port=htons(80); 
         sin.sin_addr.s_addr=inet_addr("127.0.0.1");
         sin.sin_family=AF_INET;
         if (connect(mysock,(sockaddr*)&amp;sin,sizeof(sin))==SOCKET_ERROR)
         {std::cout<<"5:(Socket Init FAiled\n"<<WSAGetLastError();
 //        getch();
         WSACleanup();
        // getch();
         return -1;
         }
     
        
              std::cout<<"connection sucessful";
              closesocket(mysock);
   
//getch();
return 0;
}   
}
When i compile and run the code the dos windows flashes for a sceond and then goes away,so i have to run this programme from the commmand line
Like this D:\>Dev-Cpp\winsock.exe
My server software:-
Code:
#include<windows.h>
#include<iostream>
int main()
{
    WSAData wsadata;
    if (WSAStartup(MAKEWORD(2,0),&amp;wsadata)!=0)
       {std::cout<<;"winsock startup  failed\n"<<WSAGetLastError();
       WSACleanup();
       return -1;
       }
     std::cout<<"winsock startup is succes\n";
     SOCKET servsock=socket(AF_INET,SOCK_STREAM,0);
     if(servsock ==INVALID_SOCKET)
     {std::cout<<"socket init failed\n";
     return -1;
     }
    std::cout<<"socket init\n"; 
    sockaddr_in sin;
    sin.sin_port=htons(80);
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_family=AF_INET;
    if (bind(servsock,(sockaddr*)&amp;sin,sizeof(sin))==SOCKET_ERROR)
    {std::cout<<"FAILED TO BIND"<<WSAGetLastError();
    WSACleanup();
    return -1;
    }
    std::cout<<"Bind successful!\n";
    WSACleanup();
    while (listen(servsock,SOMAXCONN)==SOCKET_ERROR);   //Putting the socket in listening mode 
   SOCKET client;
    int len = sizeof(sin);
    client=accept( servsock,(sockaddr*)&amp;sin,&amp;len);
    std::cout<<"Connection established!\n";
    closesocket(client);
    closesocket(servsock);
     WSACleanup();
    // getch();
   return 0;
}
When i run the software the dos windows shows "bind sucessful" but the dos windows stays there.it does not dissapear like the prevoius one
But the beginner tutorial from where i am learning said that the win-connect will display
"connection sucessful" and the win-listen will display "connection established"
when they are running at the same time .But i could not find a way to run two programmes from same compiler(by clicking on run).So i opened the the win-listen and ran the programme from the compiler((by clicking on run). and the win-connect from the command line.But i never got the required output "win-connect will display
connection sucessful and the win-listen will display connection established".So please please please can antbody tell me where i am going wrong .i am stuck with this code for 4 days, i will be really thankful whoever helps me out.And another thing is that when i am putting the socket in listening
mode:-while (listen(servsock,SOMAXCONN)==SOCKET_ERROR);
Should i keep the SOMAXCONN like this or i have to give a vaLUE LIKE 5 or 10
while (listen(servsock,10)==SOCKET_ERROR);
Please help me out brothers .Please,Please Methodz I know it pains your eye to read the code please read the code and point out the problem.
thank you

Last edited by bodhi2016; 06-23-2008 at 02:45 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-23-2008, 09:45 PM
MeTh0Dz MeTh0Dz is offline
SLICE OWNZ YOUR SOUL
 
Join Date: May 2008
Posts: 294
Last Blog:
Ternary Operator CPP
Rep Power: 0
MeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura about
Default Re: Problem with the server software

Just put some system("pause") or getch() (#include <conio.h>) at the places where your program can exit so you can so how far it is getting. Then tell me where that gets you.

Also for listen(param1, param2) you could have param2 as 1 for your purpose. All it says is how many clients can be accepted. So all you really need your server to do is accept one client.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Successfully Selling Your Software Jordan Tutorials 11 07-16-2008 06:25 PM
DNS Hacking John Tutorials 1 04-09-2008 01:34 PM
Download problem, internal Server Error j3cubcapt ionFiles 1 07-02-2007 10:45 AM


All times are GMT -5. The time now is 11:07 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads