| | |
Help on TCP Winsock
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
#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); }
Last edited by zarf; Oct 10th, 2008 at 11:06 am.
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.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
![]() |
Similar Threads
- Winsock C++ help: receiving data (C++)
- Winsock Multi-Client Servers (C++)
- Static TCP/IP Setting not saving... (Networking Hardware Configuration)
- I'm connected but cant access any sites or email (Web Browsers)
- help...ip address problem...i think? (Web Browsers)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP)
- IE TCP/IP Socket Issue? (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: C++
- Next Thread: Vector problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






