C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2001, 08:41 AM   #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
(TNT) is offline   Reply With Quote
Old 08-15-2001, 03:01 PM   #2
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
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.
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Old 08-16-2001, 02:49 AM   #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
(TNT) is offline   Reply With Quote
Old 08-16-2001, 03:34 AM   #4
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
>>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
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Old 08-18-2001, 06:59 AM   #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
headexplodes is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with simple socket client/server program spencer88 C Programming 6 05-05-2009 11:05 PM
Raw socket like_no_other Networking/Device Communication 4 03-28-2009 02:05 PM
Casting a struct for sending to socket chrisjmoss Networking/Device Communication 6 04-08-2008 09:11 AM
Having Buffer Problems With Overlapped I/O -- Sargera C++ Programming 0 02-07-2006 04:46 PM
Socket handles nico Networking/Device Communication 2 04-03-2005 10:33 PM


All times are GMT -6. The time now is 06:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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