-
December 6th, 2003, 08:54 AM
#1
Member
-SIMPLE- C Server
This is a simple little server program... my first socket program and pretty much my first program so don't bash me too badly on the formatting. You'll see that the program accepts input from the client and displays it in a message box... but what I don't understand is when I send the command quit... why does it not quit? Maybe I am doing something wrong with the while loops.. i don't know. If anyone could point me in the right direction that would be awesome. Thank you.
#include <stdio.h>
#include <winsock.h>
#include <windows.h>
#define PORT 5531
int sockfd7, testa, sockfd8, newsock, sin_size, num_bytes;
struct sockaddr_in localhost, client;
char buf[50];
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WSADATA wsda;
WSAStartup (0x0101, &wsda);
MessageBox(NULL, "Cannot Load Picture!", "Error!", 0);
if((sockfd7 = socket(AF_INET, SOCK_STREAM, 0))<0)
{
exit(1);
}
localhost.sin_family =AF_INET;
localhost.sin_port =htons(PORT);
localhost.sin_addr.s_addr =htonl(INADDR_ANY);
if((testa=bind(sockfd7, (struct sockaddr *) &localhost, sizeof(localhost)))<0)
{
closesocket(sockfd7);
exit(1);
}
listen (sockfd7, 10);
sin_size=sizeof(struct sockaddr_in);
if((newsock = accept(sockfd7, (struct sockaddr *)&client, &sin_size))==-1){
perror("accept error ");}
while(buf!="quit")
{
if((num_bytes=recv(newsock, buf, 49, 0))==-1)
{
return 0;
}
buf[ num_bytes ] = '\0';
MessageBox(NULL, buf, "Message", 0);
}
WSACleanup();
closesocket(sockfd7);
closesocket(newsock);
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|