Home > other >  Questions about the recv function based on TCP protocol
Questions about the recv function based on TCP protocol

Time:11-21

To ask questions about based on TCP protocol,
The following code is not completed, of which the service side, and the end of the service interaction as an example, the end of the service to send more than one string string, the service side when no call recv function call can only be read from the end of the service to send the first string of a string of detailed example (see figure), later will be discarded, through understanding of the recv function, the function is to use the copy function method to make a copy of the received data in the array, I assume that the input string for the service:
"111" \ n "222" \ n "333" \ n.
If there is no in the recv function to receive, then turned into "111 \ n \ 0333\0222 \ n \ n \ 0", the call to the copy function is only 111, instead of the back of the string, the actual data is not lost, if lost, can achieve the function call has been the background? The received data stored in the STL containers in the calling routine use,




# # # # # # # # # server:
# include
# include
# include
# include
# include
# include
# include
Char CD (void);
using namespace std;
Void cc (vector & A1, char a2 []);
Void xs (vector & A1);
Int main (void) {
String fs.
Char js [1024];
The SOCKET TJZ;
The SOCKET tzj1;
SOCKADDR_IN FWQ;
SOCKADDR_IN fwq1;
WORD z;
WSADATA bb;
Z=MAKEWORD (2, 2);
If (WSAStartup (z, & amp; Bb)) {
MessageBox (NULL, "error", "program errors, failed to load socket character!" , MB_OK);
exit(1);
}
If (LOBYTE (bb) wVersion)!=2 | | HIBYTE (bb) wVersion)!=2) {
MessageBox (NULL, "error", "the version number is not in conformity with the requirements of the load." , MB_OK);
exit(1);
}
FWQ. Sin_family=AF_INET;
FWQ. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
FWQ. Sin_port=htons (5000);

TJZ=socket (AF_INET SOCK_STREAM, 0).
If (bind (TJZ, (SOCKADDR *) & amp; FWQ, sizeof (SOCKADDR))=={SOCKET_ERROR)
MessageBox (NULL, "error", "binding failed!" , MB_OK);
exit(1);
}
If (listen (TJZ, 5) & lt; 0 {
MessageBox (NULL, "error", "listening failed!" , MB_OK);
exit(1);
}
{
System (" color b0 ");
Cout & lt; <"Waiting for the end of the service into the... ";
}
Int dx=sizeof (SOCKADDR);
TJZ=accept (TJZ, (SOCKADDR *) & amp; Fwq1, & amp; Dx);
If (TJZ==SOCKET_ERROR) {
MessageBox (NULL, "error", "accept connections fail!" , MB_OK);
exit(1);
}
Vector Q1 {content is as follows: ""};
While (1) {
The switch (CD ()) {
Case 'A' :
For (int w1=0; W1 & lt; 10; W1 + +) {
The recv (TJZ, js, 1023, 0);
Cc (q1, js);
Xs (q1);
}
break;
Case 'B' :
Xs (q1);
system("pause");
break;
Case 'C' : break;
}
}
return 0;
}
CD (void) {char
Char q1;
SetConsoleTitle (" welcome! -> Menu ");
System (" CLS ");
System (" color b0 ");
Cout & lt; <"Welcome to the chat system (server) \ n \ n";
Cout & lt; <"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \ n";
Cout & lt; <"# \ n";
Cout & lt; <"# a. queries received information! B. send messages! # \ n ";
Cout & lt; <"# c. exit system! D. to be added! # # \ n ";
Cout & lt; <"# # \ n";
Cout & lt; <"# # \ n";
Cout & lt; <"# \ n";
Cout & lt; <"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \ n";
Cout & lt; Q1=toupper (getchar ());
While ((getchar ())!='\ n');
If (q1 & lt; 'A' | | q1 & gt; 'C') {
MessageBox (NULL, "choose wrong, return to the menu!" ", "warning", MB_OK);
System (" CLS ");
Q1=(CD);
}
Return q1;
}
Void cc (vector & Char a1, a2 []) {
System (" CLS ");
String a3=a2;
If (a3!=* (a1. The end () - 1))
A1. The push_back (a3);
}
Void xs (vector & A1) {
System (" CLS ");
Ostream_iterator & lt; String, char> A (cout, "\ n");
Copy (a1. The begin (), a1. The end (), A);

}


# # # # # # # # # # the end of the service:
# include
# include
# include
# include
# include
using namespace std;
Int main (void) {
String fs.
Char js [100];
The SOCKET TJZ;
SOCKADDR_IN FWQ;
WORD z;
WSADATA bb;
Z=MAKEWORD (2, 2);
If (WSAStartup (z, & amp; Bb)) {
MessageBox (NULL, "bugs, failed to load socket character!" ", "error", MB_OK);
exit(1);
}
If (LOBYTE (bb) wVersion)!=2 | | HIBYTE (bb) wVersion)!=2) {
MessageBox (NULL, "the version number is not in conformity with the requirements of the load." ", "error", MB_OK);
exit(1);
}
FWQ. Sin_family=AF_INET;
Char qq [20];
Cout & lt; <"Enter IP:";
Cin & gt;> Qq;
FWQ. Sin_addr. S_un. S_addr=inet_addr (qq).
FWQ. Sin_port=htons (5000);
TJZ=socket (AF_INET SOCK_STREAM, 0).
If (connect (TJZ, (SOCKADDR *) & amp; FWQ, sizeof (SOCKADDR))=={SOCKET_ERROR)
MessageBox (NULL, "connect failed!" ", "error", MB_OK);
exit(1);
}
While (1) {
Cout & lt; <"The connection is successful, the input information, end of line breaks:";
Cin & gt;> The fs.
Send (TJZ, fs. C_str (), 100, 0).
}
return 0;
}


nullnull
  • Related