0% found this document useful (0 votes)
24 views

"Stdafx.H" : #Include #Include Typedef Struct

The document describes a multi-threaded server program that accepts client connections, handles client requests concurrently using separate threads, and returns responses to clients. When a client connects, the server creates two threads - one to receive data from the client and perform calculations, and another to send responses back to the client. It uses events to synchronize data exchange between the threads handling a particular client connection.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

"Stdafx.H" : #Include #Include Typedef Struct

The document describes a multi-threaded server program that accepts client connections, handles client requests concurrently using separate threads, and returns responses to clients. When a client connects, the server creates two threads - one to receive data from the client and perform calculations, and another to send responses back to the client. It uses events to synchronize data exchange between the threads handling a particular client connection.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

---------------------------SEVER--------------------------------------------// sumserver.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <WinSock2.

h> typedef struct _CLIST { SOCKET c; SOCKADDR_IN addr; int addrlen; char* buf; char* ret; _CLIST* next; HANDLE hReady; }CLIST; SOCKADDR_IN serverAddr; SOCKET s; WSADATA wsaData; DWORD ThreadID; CLIST* pClients = NULL; DWORD WINAPI SendingThread(LPVOID param) { CLIST* p = (CLIST*)param; while (1) { WaitForSingleObject(p->hReady,INFINITE); send(p->c,p->ret,strlen(p->ret),0); Sleep(1); } return 0; } DWORD WINAPI RecvingThread(LPVOID param) { CLIST* p = (CLIST*)param; p->buf = (char*)calloc(1024,1); p->ret = (char*)calloc(1024,1); while (1) { ResetEvent(p->hReady); recv(p->c,p->buf,1024,0); int a,b,c; sscanf(p->buf,"%d%d",&a,&b); c = a + b; memset(p->ret,0,1024); sprintf(p->ret,"%d\n",c); SetEvent(p->hReady); Sleep(1); } return 0; } DWORD WINAPI ListeningThread(LPVOID param) { pClients = (CLIST*)calloc(1,sizeof(CLIST));

while (1) { pClients->addrlen = sizeof(pClients->addr); pClients->c = accept(s,(sockaddr*)&pClients->addr,&pClients->addrlen); pClients->hReady = CreateEvent(NULL,TRUE,FALSE,NULL); CreateThread(NULL,0,SendingThread,pClients,0,&ThreadID); CreateThread(NULL,0,RecvingThread,pClients,0,&ThreadID); pClients->next = (CLIST*)calloc(1,sizeof(CLIST)); pClients = pClients->next; } return 0; } int main(int argc, char* argv[]) { WSAStartup(MAKEWORD(2,2),&wsaData); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(atoi(argv[1])); serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); bind(s,(sockaddr*)&serverAddr,sizeof(serverAddr)); listen(s,10); WaitForSingleObject(CreateThread(NULL,0,ListeningThread,NULL,0,&ThreadID), INFINITE); return 0; }

-------------------------------------------CLIENT----------------------------------------------// sumclient.cpp : Defines


the entry point for the console application. // #include "stdafx.h" #include <WinSock2.h> SOCKADDR_IN serverAddr; SOCKET s; WSADATA wsaData; char* line = NULL; char* ret = NULL; int main(int argc, char* argv[]) { WSAStartup(MAKEWORD(2,2),&wsaData); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(atoi(argv[2])); serverAddr.sin_addr.s_addr = inet_addr(argv[1]); s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); line = (char*)calloc(1024,1); ret = (char*)calloc(1024,1); if (connect(s,(sockaddr*)&serverAddr,sizeof(serverAddr)) == 0) { while (1) { printf("Nhap vao lenh: ");

gets(line); send(s,line,strlen(line),0); recv(s,ret,1024,0); printf("%s\n",ret); } }else { printf("Cannot connect"); } return 0; }

You might also like