Socket Problems

This is a discussion on Socket Problems within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am trying to create a basic socket app, in it the server will send a msg to the ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Cool Socket Problems

    Hi,

    I am trying to create a basic socket app, in it the server will send a msg to the client then the client will display the msg in a message box.

    Here is my code for the server & client app:

    Code:
    //client app
    //these defs are under the #includes
    #define PORT 3550
    #define MAXDATASIZE 100
    
    struct       hostent *he;        
    struct       sockaddr_in server;    
    int      numbytes;      	   
    char         buf[MAXDATASIZE];     
    
    WSADATA  wsdata;
    SOCKET   sock;
    DWORD    wsock;
    
    //then from a button message the main sock code
       WSAStartup(0x0101,&wsdata); 
       he = gethostbyname("localhost");      	      	   
       sock = socket(AF_INET, SOCK_STREAM, 0);
       server.sin_family = AF_INET;
       server.sin_port = htons(PORT);      	                
       server.sin_addr = *((struct in_addr *)he->h_addr);    
       connect(sock,(struct sockaddr *)&server,sizeof(struct sockaddr));
     
       numbytes = recv(sock, buf, MAXDATASIZE, 0));
       buf[numbytes] = '\0';
       MessageBox(0,buf,"The Servers Msg",0);
       WSACleanup();

    This is the server app:

    Code:
    //defs under #includes
    #define PORT 3550   
    #define BACKLOG 1 
    
    struct sockaddr_in server;
    struct sockaddr_in client;
    int sin_size;
    WSADATA  wsdata;
    SOCKET   sock1, sock2;
    DWORD    wsock;
    
    //the main socket code from a button msg
    WSAStartup(0x0101,&wsdata);  
    sock1 = socket(AF_INET, SOCK_STREAM, 0);
    
    server.sin_family = AF_INET;         
    server.sin_port = htons(PORT);         
    server.sin_addr.s_addr = INADDR_ANY; 
    
    bind(sock1,(struct sockaddr*)&server,sizeof(struct sockaddr));
    listen(sock1, BACKLOG);
    
    while(1)
    {
     sin_size = sizeof(struct sockaddr_in);
     sock2 = accept(sock1,(struct sockaddr *)&client,&sin_size);
          
    MessageBox(0,"Client connected to server.","Connection",0);
    send(sock2,"This Msg Is From The Server",22,0); // send msg to client
    WSACleanup();
       return -1;
    }
    Both the server and the client compile fine and i can get them to connect, i know this becasue i got the message from the server, Client Connected to server.

    The problem i have is with the sending & reciving.
    Basically the client does not get the message that the server sends to it. Does anyone know why i do not get the server msg?

    Also a smaller problem was that when the server started listing, the program sort of crashed in that i coudnt close it, i know this is becasue of the endless while loop, but is there a way around this?

    Thanks in advance
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    OK....

    have a try with this for the client;

    Code:
    #include <winsock2.h>
    #include <stdio.h>
    #define PORT 3550
    #define MAXDATASIZE 100
    
    struct       hostent *he;        
    struct       sockaddr_in server;    
    int            numbytes;      	   
    char         buf[MAXDATASIZE];     
    
    WSADATA  wsdata;
    SOCKET   sock;
    DWORD    wsock;
    int main(void)
    {
       
    
       WSAStartup(0x0101,&wsdata); 
            	      	   
       sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      
       he = gethostbyname("localhost"); 
       server.sin_family = AF_INET;
       server.sin_port = htons(PORT);      	                
       server.sin_addr = *((struct in_addr *)he->h_addr);    
     
       if(connect(sock,(struct sockaddr *)&server,sizeof(server))==SOCKET_ERROR)
       {
    	   MessageBox(0,"Connect Error","",MB_OK);
    	   return 0;
       }
       
       numbytes = recv(sock, buf, MAXDATASIZE, 0);
       buf[numbytes] = '\0';
      
       MessageBox(0,buf,"Server response:",MB_OK);
       closesocket(sock);
       WSACleanup();
       return 1;
    }
    now the server;

    Code:
    #include <winsock2.h>
    #include <stdio.h>
    #define PORT 3550   
    #define BACKLOG 1 
    
    struct sockaddr_in server;
    struct sockaddr_in client;
    int sin_size;
    WSADATA  wsdata;
    SOCKET   sock1, sock2;
    DWORD    wsock;
    char success[] = "This Msg Is From The Server", buf[50];
    int r;
    
    int main(void){
    
    
    WSAStartup(0x0101,&wsdata);  
    sock1 = socket(AF_INET, SOCK_STREAM, 0);
    
    server.sin_family = AF_INET;         
    server.sin_port = htons(PORT);         
    server.sin_addr.s_addr = INADDR_ANY; 
    
     bind(sock1,(struct sockaddr*)&server,sizeof(struct sockaddr));
     listen(sock1, BACKLOG);
    
    
     sin_size = sizeof(struct sockaddr_in);
     sock2 = accept(sock1,(struct sockaddr *)&client,&sin_size);
     printf("Client connected to server.");
     r = send(sock2,success,strlen(success),0); // send msg to client
       
     closesocket(sock1);
     closesocket(sock2);
     WSACleanup();
     return 1;
    }
    Compile them both as consoles (didnt use winmain cuz I'm lazy). Make sure you add the WS2_32.lib to both projects.

    Run server in console window (should just hang there). Then run client in another console window. The server window should finish running and display "Client connected to server.". There should also be a messagebox with a string passed from the server to the client - I think this is what you were after.

    I quickly tested it on my PC and it seems to work, but if not then say.

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Cool K still a small problem

    Yo,

    Thanks it sends the message through now but still while the server is listing it kinda crashes, but when a connection is made it becomes usable agai. But while it is listning it is crashed.

    Does anyone know how i can solve this problem?

    Thanks
    TNT
    You Can Stop Me, But You Cant Stop Us All

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>but still while the server is listing it kinda crashes

    Its not really crashed, its just hanging while it waits for a response. If you want to add functionality to the server while its waiting, then you want to put the listen function in its own thread (I use _beginthread cuz I've got VC++, but you can use CreateThread if you have another compiler). This thread will then operate independent of the main process and only affect the main thread when someone connects to the server, but while its listning you can do other things with the main process.

    Also, if you want it to transfer more than a simple string - this would be a good situation for another thread.

    Hope this is what you want

  5. #5
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26
    I had an application that had a constant loop too. (except it was painting graphics constantly). I had the same problem of windows thinking it was hung. So.. i created a thread but then I couldnt figure out how to stop the application ending and causing the thread to terminate as well. Is there a simple way to stop this?
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  2. Raw socket
    By like_no_other in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-28-2009, 03:05 PM
  3. Casting a struct for sending to socket
    By chrisjmoss in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-08-2008, 10:11 AM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  5. Socket handles
    By nico in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-03-2005, 11:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21