943,609 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1634
  • C++ RSS
Oct 10th, 2008
0

Help on TCP Winsock

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zarf is offline Offline
2 posts
since Oct 2008
Oct 10th, 2008
0

Re: Help on TCP Winsock

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Oct 11th, 2008
0

Re: Help on TCP Winsock

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zarf is offline Offline
2 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++
Next Thread in C++ Forum Timeline: Vector problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC