Hi all i am a newbie in C++, i am currently doing my FYP and i need some help on it. I am using visual C++ version 6.0.
Firstly, i have 2 machines A ( the client ) and B ( the server ), A is supposed to send a signal to B. The problem is i want to combine or link 2 programs, which is a TCP Winsock Server and a record program together in B.
The record program has a code which says OnButtonRecord() , whereby when the program is executed, the user have to click the button himself to start the record function. How do i get the TCP Winsock Server to trigger the record program to start auto-record? ( When Winsock Server receives a signal from A, it tells the record program to start recording , without having the user to press the button manually).
Can i remove the OnButtonRecord and replace it with some other codes?
Also how do i set a static ip address in the code because the Winsock Server requires the user to type an ip address first, i would like to make it a default ip.

Forgive me if it sounds confusing, i'm not really sure how i should explain it, any help would be appreciated, thanks a lot.

The code for the Winsock TCP server:

#include "stdafx.h"
#include <Winsock2.h>
#include <stdio.h>
#include <iostream>
#include <process.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
SOCKET AcceptSocket;
SOCKET  ListenSocket;
fd_set fdd;
int count;

 VOID mThread (PVOID pvoid)      
{
  int ret;
   printf("begin receive message...\r\n") ;
  char ar[1024];

  while(1)
  {
	  
	  ret=select(0,&fdd,NULL,NULL,NULL);
	  if(ret>0)
	  {
	  recv(fdd.fd_array[ret-1],ar,1024,0);
	  if(send(AcceptSocket,ar,128,NULL)==SOCKET_ERROR)
				printf("failed to send\n");
	  count++;
	  cout<<"Receive from client A "<<count<<" data£¬the result is "<<(int)ar[0]<<" £¬transferring to C"<<endl;
	  }
  }
}

VOID Thread (PVOID pvoid)      
{
	unsigned long ul=1;
	long ull=1;
	while(true)
	{
		printf("waiting for connection\n") ;
		
		
		AcceptSocket = accept( ListenSocket, NULL, NULL );
		ioctlsocket(AcceptSocket,ul,(unsigned long*)&ul);
		if (AcceptSocket == INVALID_SOCKET)
		{
			printf("AcceptSocket error\r\n") ;
			closesocket(ListenSocket);
			WSACleanup();
			return;
		} 
		else
		{
			printf("client connction...\r\n") ;
			FD_ZERO(&fdd);
			FD_SET(AcceptSocket,&fdd);
			_beginthread (mThread, 0, NULL);
		}
	}	
}

void main()
{
	cout<<"startup Controler B, Please set up IP address :";zz
	char addrB[30];
	gets(addrB);
	WSADATA wsaData;
	int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (iResult != NO_ERROR)
	{
		printf("Error at WSAStartup()\n");
	    return;
	}
	ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ListenSocket == INVALID_SOCKET) 
	{
		printf("Error at socket(): %ld\n");
		WSACleanup();
		return;
	}
	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr(addrB);
	service.sin_port = htons(27015);
	if (bind( ListenSocket,(SOCKADDR*) &service,sizeof(service)) == SOCKET_ERROR)
	{
		printf("bind() failed.\n");
		closesocket(ListenSocket);
		return;
	}
	if(listen(ListenSocket,10)==SOCKET_ERROR)
	{
		printf("listen() failed.\n");
		closesocket(ListenSocket);
		return;
	}
	_beginthread (Thread, 0, NULL);
	char ccc[8];
	gets(ccc);
}

Are you combining client and server into just one program ? Or are they still two separate programs that are executed on the same machine ?

>>Also how do i set a static ip address in the code because the Winsock Server requires the user to type an ip address first, i would like to make it a default ip.

Just set the variable used for the manual entry to be "127.0.0.1", which is always the local machine.

Are you combining client and server into just one program ? Or are they still two separate programs that are executed on the same machine ?

>>Also how do i set a static ip address in the code because the Winsock Server requires the user to type an ip address first, i would like to make it a default ip.

Just set the variable used for the manual entry to be "127.0.0.1", which is always the local machine.

Hi, thanks for the reply. Yes they are 2 separate programs that are executed on the same machine. This Winsock TCP Server itself is an .exe whereby the user have to type in an ip address and the record program has an OnButton function that requires the user to press a button so it can start working. What i would like to do is to remove this OnButton function and then use the Server code to notify it to start auto-recording.

The reason why i would i like to set a static ip is because i do not know how to execute them at the same time, if i can set a static ip address within the server code, then there won't be a need to prompt the user for an ip address, hence allowing the Server and record program to run at the same time.

This article has been dead for over six months. Start a new discussion instead.