View Single Post
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