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);
}