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:
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.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; }
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



LinkBack URL
About LinkBacks




hotmail.com */