• 0

[C++] library of socket


Question

12 answers to this question

Recommended Posts

  • 0

i read a lot of tutorials and i write this for server and client, but...

SERVER
  SOCKET s, incoming;
  WSADATA wsaData;
  SOCKADDR_IN sockAddr, incomingSockAddr;
  char buffer[2048];
  int length;


  WSAStartup(0x0101, &wsaData);
  s = socket(AF_INET, SOCK_STREAM, 0);

  sockAddr.sin_family = PF_INET;
  sockAddr.sin_port = htons(5001);
  sockAddr.sin_addr.s_addr = INADDR_ANY;

  bind(s,(SOCKADDR *)&sockAddr, sizeof(SOCKADDR_IN));
  listen(s, SOMAXCONN); // SOMAXCONN means the maximun reasonable amount

  length = sizeof(SOCKADDR_IN);
  incoming = accept(s, (SOCKADDR *)&incomingSockAddr, &length);

  while(length != 0) {
 	 length = recv (incoming, buffer, 2048,  0);
 	 printf(" %s ",buffer);}
  shutdown (s, SD_BOTH);  // s cannot send anymore
  closesocket (s);   // close

CLIENT
  SOCKET s;
  WSADATA wsaData;
  SOCKADDR_IN sockAddr, incomingSockAddr;
  WSAStartup(0x0101, &wsaData);

  sockaddr_in target;

  target.sin_family = AF_INET;           // address family Internet
  target.sin_port = htons (5001);        // set server's port number
  target.sin_addr.s_addr = inet_addr ("52.123.72.251");  // set server's IP

  connect(s, target, sizeof(target));     //<---- HERE THE ERROR //

  char buffer[2048];
  sprintf(buffer,"I do what my master want!");
  send (s, buffer, sizeof(buffer), 0);
  
  shutdown (s, SD_BOTH);  // s cannot send anymore
  closesocket (s);   // close

he gives me this error of type:

error C2664: 'connect' : cannot convert parameter 2 from 'sockaddr_in' to 'const sockaddr *'

but all tutorial do it.

Tnx a lot

  • 0

excuse me i do not explain myself,

with this code i create a TCP connection?

so if i send something whit client it stop itself while the server recive the data or no?

 s = socket(AF_INET, SOCK_STREAM, 0);

  sockAddr.sin_family = PF_INET;
  sockAddr.sin_port = htons(5001);
  sockAddr.sin_addr.s_addr = INADDR_ANY;

  bind(s,(SOCKADDR *)&sockAddr, sizeof(SOCKADDR_IN));
  listen(s, SOMAXCONN); // SOMAXCONN means the maximun reasonable amount

  • 0
  maniak said:
so if i send something whit client it stop itself while the server recive the data or no?

no. the client won't wait for server to recieve the data. it; but you can do the following:

send message to server

wait a confirmation message from server

please look at select function for more and try those links :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.