Hello, I was working on my Server - Client comunicating programm for moonth or more, when a tricky issue had met me. I works so: Server does certain commands if client types certain commnads, and I need need the Client to do certain commands if Server types certain commands. I tried to do it the same way as Server receives the commands, but it didn't work.
This is working code, but I need to reverse it:
::--Server--::
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <iostream.h>
#include <conio.h>
#include <signal.h>
DWORD WINAPI receive_cmds(LPVOID lpParam)
{
SOCKET current_client = (SOCKET)lpParam;
char buf[100];
char sendData[100];
int res;
while(true)
{
res = recv(current_client,buf,sizeof(buf),0); // recv cmds
Sleep(10);
if(res == 0)
{
MessageBox(0,"error","error",MB_OK);
closesocket(current_client);
ExitThread(0);
}
if(strstr(buf,"hello"))
{ printf("\nrecived hello cmd");
strcpy(sendData,"hello, greetz from KOrUPt\n");
Sleep(10);
send(current_client,sendData,sizeof(sendData),0);
}
else if(strstr(buf,"bye"))
{ // dissconnected this user
printf("\nrecived bye cmd\n");
strcpy(sendData,"cya\n");
Sleep(10);
send(current_client,sendData,sizeof(sendData),0);
// close the socket associted with this client and end this thread
closesocket(current_client);
ExitThread(0);
}
else
{
strcpy(sendData,"Invalid cmd\n");
Sleep(10);
send(current_client,sendData,sizeof(sendData),0);
}
// clear buffers
strcpy(sendData,"");
strcpy(buf,"");
}
}
int main()
{
printf("Starting up server\r\n");
SOCKET sock;
DWORD thread;
WSADATA wsaData;
sockaddr_in server;
int ret = WSAStartup(0x101,&wsaData); // use highest version of winsock avalible
if(ret != 0)
{
return 0;
}
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(15065); // listen on telnet port xxx
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
return 0;
}
// bind our socket to a port
if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
{
return 0;
}
// listen for a connection
if(listen(sock,5) != 0)
{
return 0;
}
SOCKET client;
sockaddr_in from;
int fromlen = sizeof(from);
while(true)
{
client = accept(sock,(struct sockaddr*)&from,&fromlen);
printf("Client connected\r\n");
CreateThread(NULL, 0,receive_cmds,(LPVOID)client, 0, &thread);
}
closesocket(sock);
WSACleanup();
return 0;
}
::--Client--::
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <signal.h>
#include <stdio.h>
#define SIGINT 2
#define SIGKILL 9
#define SIGQUIT 3
SOCKET sock,client;
void s_handle(int s)
{
if(sock)
closesocket(sock);
if(client)
closesocket(client);
WSACleanup();
Sleep(1000);
cout<<"EXIT SIGNAL :"<<s;
exit(0);
}
void s_cl(char *a, int x)
{
cout<<a;
s_handle(x+1000);
}
int main()
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
SetConsoleTitle("Client");
//Declarations
DWORD poll;
int res,i=1,port=999;
char buf[100];
char msg[100] = "";
char ip[15]= "62.119.44.70";
WSADATA data;
signal(SIGINT,s_handle);
signal(SIGKILL,s_handle);
signal(SIGQUIT,s_handle);
cout<<"Client";
sockaddr_in ser;
sockaddr addr;
ser.sin_family=AF_INET;
ser.sin_port=htons(15065);
ser.sin_addr.s_addr=inet_addr(ip);
memcpy(&addr,&ser,sizeof(SOCKADDR_IN));
res = WSAStartup(MAKEWORD(1,1),&data);
cout<<"\n\nWSAStartup"
<<"\nVersion: "<<data.wVersion
<<"\nDescription: "<<data.szDescription
<<"\nStatus: "<<data.szSystemStatus<<endl;
if(res != 0)
s_cl("WSAStarup failed",WSAGetLastError());
sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock==INVALID_SOCKET )
s_cl("Invalid Socket ",WSAGetLastError());
else if(sock==SOCKET_ERROR)
s_cl("Socket Error)",WSAGetLastError());
else
cout<<"Socket Established"<<endl;
res=connect(sock,&addr,sizeof(addr));
if(res !=0 )
{
s_cl("SERVER UNAVAILABLE",res);
//restart client
}
else
{
cout<<"\nConnected to Server: ";
memcpy(&ser,&addr,sizeof(SOCKADDR));
}
char RecvdData[100] = "";
int ret;
while(true)
{
strcpy(buf,"");
cout<<"\nEnter command ->\n";
fgets(buf,sizeof(buf),stdin);
Sleep(5);
res = send(sock,buf,sizeof(buf),0);
if(res==0)
{
//0==other side terminated conn
printf("\nSERVER terminated connection\n");
Sleep(40);
closesocket(client);
client = 0;
break;
}
else if(res==SOCKET_ERROR)
{
//-1 == send error
printf("Socket error\n");
Sleep(40);
s_handle(res);
break;
}
ret = recv(sock,RecvdData,sizeof(RecvdData),0);
if(ret > 0)
{
cout<<endl<<RecvdData;
strcpy(RecvdData,"");
}
}
closesocket(client);
WSACleanup();
}
Please help me to figure t out, I'll be very thankful for each reply :)