Help on TCP Winsock

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 2
Reputation: zarf is an unknown quantity at this point 
Solved Threads: 0
zarf zarf is offline Offline
Newbie Poster

Help on TCP Winsock

 
0
  #1
Oct 10th, 2008
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:
  1. #include "stdafx.h"
  2. #include <Winsock2.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <process.h>
  6. using namespace std;
  7. #pragma comment(lib,"ws2_32.lib")
  8. SOCKET AcceptSocket;
  9. SOCKET ListenSocket;
  10. fd_set fdd;
  11. int count;
  12.  
  13. VOID mThread (PVOID pvoid)
  14. {
  15. int ret;
  16. printf("begin receive message...\r\n") ;
  17. char ar[1024];
  18.  
  19. while(1)
  20. {
  21.  
  22. ret=select(0,&fdd,NULL,NULL,NULL);
  23. if(ret>0)
  24. {
  25. recv(fdd.fd_array[ret-1],ar,1024,0);
  26. if(send(AcceptSocket,ar,128,NULL)==SOCKET_ERROR)
  27. printf("failed to send\n");
  28. count++;
  29. cout<<"Receive from client A "<<count<<" data£¬the result is "<<(int)ar[0]<<" £¬transferring to C"<<endl;
  30. }
  31. }
  32. }
  33.  
  34. VOID Thread (PVOID pvoid)
  35. {
  36. unsigned long ul=1;
  37. long ull=1;
  38. while(true)
  39. {
  40. printf("waiting for connection\n") ;
  41.  
  42.  
  43. AcceptSocket = accept( ListenSocket, NULL, NULL );
  44. ioctlsocket(AcceptSocket,ul,(unsigned long*)&ul);
  45. if (AcceptSocket == INVALID_SOCKET)
  46. {
  47. printf("AcceptSocket error\r\n") ;
  48. closesocket(ListenSocket);
  49. WSACleanup();
  50. return;
  51. }
  52. else
  53. {
  54. printf("client connction...\r\n") ;
  55. FD_ZERO(&fdd);
  56. FD_SET(AcceptSocket,&fdd);
  57. _beginthread (mThread, 0, NULL);
  58. }
  59. }
  60. }
  61.  
  62. void main()
  63. {
  64. cout<<"startup Controler B, Please set up IP address :";zz
  65. char addrB[30];
  66. gets(addrB);
  67. WSADATA wsaData;
  68. int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  69. if (iResult != NO_ERROR)
  70. {
  71. printf("Error at WSAStartup()\n");
  72. return;
  73. }
  74. ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  75. if (ListenSocket == INVALID_SOCKET)
  76. {
  77. printf("Error at socket(): %ld\n");
  78. WSACleanup();
  79. return;
  80. }
  81. sockaddr_in service;
  82. service.sin_family = AF_INET;
  83. service.sin_addr.s_addr = inet_addr(addrB);
  84. service.sin_port = htons(27015);
  85. if (bind( ListenSocket,(SOCKADDR*) &service,sizeof(service)) == SOCKET_ERROR)
  86. {
  87. printf("bind() failed.\n");
  88. closesocket(ListenSocket);
  89. return;
  90. }
  91. if(listen(ListenSocket,10)==SOCKET_ERROR)
  92. {
  93. printf("listen() failed.\n");
  94. closesocket(ListenSocket);
  95. return;
  96. }
  97. _beginthread (Thread, 0, NULL);
  98. char ccc[8];
  99. gets(ccc);
  100. }
Last edited by zarf; Oct 10th, 2008 at 11:06 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help on TCP Winsock

 
0
  #2
Oct 10th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: zarf is an unknown quantity at this point 
Solved Threads: 0
zarf zarf is offline Offline
Newbie Poster

Re: Help on TCP Winsock

 
0
  #3
Oct 11th, 2008
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC