This semester began to learn network programming, the first task is the hours of TCP/IP client Server Setup
Use C to write server side (Server.cpp)
#pragma comment (lib, "Ws2_32.lib")/precompiled #include <stdio.h> #include <winsock2.h> int main (int argc, char *arg
V[]) {//step1: Initialize Windows Socket 2.2 library Wsadata Wsadata;
WSAStartup (Makeword (2,2), &wsadata);
Step2: Create a new socket to respond to the client's connection request socket Slisten = socket (af_inet, sock_stream, ipproto_tcp);
Step3: Fill in the server address information int port = 5150;//port is 5150 sockaddr_in serveraddr;
serveraddr.sin_family = af_inet;
Serveraddr.sin_port = htons (port);
ServerAddr.sin_addr.s_addr =inet_addr ("10.66.103.187");
SERVERADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); IP address is inaddr_any, use htonl to convert IP address to network format//STEP4: Bind listening Port bind (Slisten, (SOCKADDR *) &serveraddr, sizeof (SERVERADDR))
;
STEP5: Start listening, specify three times handshake waiting queue Length of 5 listen (Slisten, 5);
printf ("Start listening ... \ n");
while (true) {//step6: Accept the new connection SOCKET snewconnection;
Sockaddr_in clientaddr;
memset (&clientaddr, 0, sizeof (CLIENTADDR));
int clientaddrlen = sizeof (CLIENTADDR); Snewconnection =Accept (Slisten, (SOCKADDR *) &clientaddr, &clientaddrlen);
STEP7: After the new connection is established, you can communicate with each other,//In this simple example,//We send "Welcome to network programing!" and then close the connection//data exchange while (1) {
Char message[1024];
int recvsize = recv (snewconnection, message, sizeof (message), 0);
if (recvsize <= 0) break;
printf ("%s:%s\n", "Hu", message);
Char data[1024] = {0};
printf ("%s:", "Li");
scanf ("%s", data);
Send (snewconnection, data, strlen (message) +1, 0);
//Close Socket closesocket (snewconnection) communicating with customers;
printf ("%s", "close on the other end");
//STEP8: Closes socket closesocket (slisten);
STEP9: Release related resources for Windows Socket DLL wsacleanup ();
return 0; }
Then the client (client.cpp)
#include <stdio.h> #include <winsock.h> #pragma comment (lib, "Ws2_32.lib")/
The previous meaning of this sentence is static join a Lib file that is the library file Ws2_32.lib file, provides support for the following network-related APIs, if you use the API, you should add Ws2_32.lib to the project (otherwise you need to dynamically load ws2_32.dll).
int main (int argc, char *argv[]) {//int first,second; Step 1: Initialize Windows Socket 2.2 Library Wsadata wsadata;//feature is: Store windows socket initialization Information WSAStartup (Makeword (2,2), &wsad
ATA);
Step 2: Create client socket sockets Socket sclient = socket (af_inet, sock_stream, 0);
Step 3: Set the server address sockaddr_in serveraddr;
memset (&serveraddr, 0, sizeof (sockaddr_in));/Remove no effect, temporarily unknown use/*void *memset (void *s, int ch, size_t n);
function Explanation: Replace n bytes (typedef unsigned int size_t) after the current position in s with ch and return S.
Memset: The function is to fill a given value in a block of memory, which is the fastest way to clear 0 operations on a larger structure or array. * * serveraddr.sin_family = af_inet;
SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Serveraddr.sin_port = htons ((u_short) atoi (argv[2));
ServerAddr.sin_addr.s_addr =inet_addr ("127.0.0.1"); SERVERADDR.SIN_ADDR.S_ADDR = htonl (inaddr_any);
Serveraddr.sin_port = htons (5150); Step 4: Connect to the server connect (sclient, (SOCKADDR *) &serveraddr, sizeof (SERVERADDR)); the difference between//sockaddr and sockaddr_in:
The memory size of the two is the same, so they can be converted to each other, in this sense, they do not distinguish//step 5: Communication with the server while (1) {char data[1024] = {0};
printf ("%s:", "Hu");
scanf ("%s", data); int sendsize = Send (sclient, data, strlen (data) +1, 0);//The third parameter is the length of the data in the buffer, returning the total length of the data sent or the socket_error error int recvsize = recv ( Sclient, data, 1024, 0);//The third parameter is the length of the buffer, and the number of bytes received when the execution succeeds. The other end is closed and returns 0.
Failure returned-1 printf ("%s:%s\n", "Li", data);
//step 6: Close socket closesocket (sclient);
Step 7: Release Winsock library WSACleanup ();
GetChar ();
GetChar ();
return 0; }
Finally, the test results: