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 :)

I wrote a fairly substantial two-way socket-based communication program over a year ago. If you have a basic client-server model working (server listens for connections on a particular port, client [or multiple clients] opens socket connections to the server's port, issues a command, reads until a response returns, and then maybe issues another command, and so on, and closes the socket when done), then you don't need to "reverse" much.

Assume that the "client" still controls the socket connection and will close it when finished. Then in order not to block while waiting for input, each side needs a loop that checks whether there is input waiting to be read or output waiting to be sent. If the socket communication is placed in its own thread, then a queue can be used to store up outbound messages until the socket loop is ready to send them. If there's any chance that data in one direction could prevent data from going the other way in a timely manner, then use a flag variable to switch between "check for something to receive" and "check for something to send" and bounce back and forth each time through the loop. Getting the code correct is not trivial! See how far you can get with this much. Good luck!

commented: Very good ! and helpful +0

Thanks for help, I have figured it out (after like 1 week of my very effort).
It's now working and ready to use for ****cious purposes :D

Glad to hear it! Please mark your thread as "solved". :)

Im sorry, but IDK how ... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.