Iam facing one problem in my socket programming on a server based program.Everything thing seems to be fine i meant my server & client are getting connected but on the client side i cannot see the values which i am sending . Code: // WAS_Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "WAS_Server.h" #include "winsock2.h" #include "conio.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; UINT ClientThread(LPVOID pParam); UINT MTServerThread(LPVOID pParam); //BOOL Recfile(SOCKET,CString); //BOOL FileSearch(SOCKET,CString,CString); HANDLE hmtx1 ; //BOOL ParseCmd(char* str,CString& cmd,CString& params); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {/* unsigned char type=1011; unsigned char nom=001; unsigned char off=0; unsigned char first; nom=nom<<4; off=off<<7; first=type|nom|off; printf("%d",first);*/ int nRetCode = 0; hmtx1 = CreateMutex(NULL,NULL,NULL) ; cout<<"Press Esc to terminate the program\r\n"; SOCKET server; AfxBeginThread(MTServerThread,(LPVOID)server); while(_getch()!=27); closesocket(server); WSACleanup(); return nRetCode; } UINT MTServerThread(LPVOID pParam) { SOCKET server=(SOCKET)pParam; sockaddr_in local; WSADATA wsadata ; int wsaret = WSAStartup(0x101,&wsadata) ; if(wsaret!=0) return 0; local.sin_family=AF_INET; local.sin_addr.s_addr=INADDR_ANY; local.sin_port=htons((u_short)15002); server=socket(AF_INET,SOCK_STREAM,0); if(server==INVALID_SOCKET) { return 0; } if(bind(server,(sockaddr*)&local,sizeof(local))!=0) { return 0; } if(listen(server,10)!=0) { int gle = WSAGetLastError(); printf("listening failure:%d\n", gle); getch(); return 0; } SOCKET client; sockaddr_in from; int fromlen=sizeof(from); printf("Server started\n"); while(1) { client=accept(server,(struct sockaddr*)&from,&fromlen); AfxBeginThread(ClientThread,(LPVOID)client); } return 0; } UINT ClientThread(LPVOID pParam) { char buff[10]={0x1b,0x81,0x00,0x80,0x00,0x80,0x40,0x00,0x00,0x1}; char buffr[1024]; CString cmd; int sb; int gle; CString params; int n; BOOL auth=false; CString npath; //memset(buff,0,10) ; //unsigned char *val; //val = buff; SOCKET client=(SOCKET)pParam; //strcpy(buff,"c"); if(sb = send(client,buff,strlen(buff),0)> 0); { gle = WSAGetLastError(); printf("sent:%dbytes\nle = %d\n", sb, gle);} while(1) { char *recb = (char*)malloc(1024); memset(recb, 0, 1024); n=recv(client,buffr,1024,0) ; gle = WSAGetLastError(); if(n==SOCKET_ERROR) {printf("\nsocket error"); break;} else { buffr[n]=0; printf("received from client:%dbytes, %d\n", n, gle); sprintf(recb, "%x", buffr); printf(recb); free(recb); break; } } closesocket(client); return 0 ; } In the output iam getting Code: press Esc to terminate Server started sent ": 1bytes le =0 received from client : 0 bytes ,0 f8fa84 It seems only first byte is getting transferred but when i am getting disconnected with my client iam getting f8fa84. Well dont understand this value means .Kindly give some solution
Look to me the whole program is wrong for a client server model. I think the server and client are not getting connected. Not sure how can you say that You create a server thread listen on a socket accept on a socket after accept is over you start the client. accept is already over, what will server accept what server no longer is waiting for accept. check the return value of accept. one more mistake, you are ending if with a semicolon so no use of if condition if(sb = send(client,buff,strlen(buff),0)> 0); and also have client and server thread parallel not in recursion. The program to me looks in total mess. Please look at a simple client server socket program to understand the flow. and then write you code. You can use the below link for reference http://www.sockaddr.com/ExampleSourceCode.html Stream.zip :Very simple stream client and server console applications.