In Linux, TCP network programming communicates with C # socket programming based on Windows,
I. Basics of TCP network programming in linux
Socket (): used for Socket initialization.
Bind (): Bind a socket to a port on the local machine to listen to service requests on this port.
Listen (): puts the socket in passive listening mode, creates an input data queue for the socket, and stores the incoming server and requests in the queue until the program processes them.
Accept (): allows the server to receive client connection requests.
Connect (): the client uses the connect function to configure the socket and establish a TCP connection with the remote server.
Close (): Close socket
Send (): sending Function
Recv (): Accept Function
2. Shows the server and client flowchart.
Iii. TCP programming instances in linux
TCP server program
1/* server. c */2 # include <sys/types. h> 3 # include <sys/socket. h> 4 # include <sys/ioctl. h> 5 # include <netinet/in. h> 6 # include <stdio. h> 7 # include <stdlib. h> 8 # include <string. h> 9 # include <unistd. h> 10 # define PORT 2000 // define PORT Number 11 # define BUFFER_SIZE 1024 12 # define MAX_QUE_CONN_NM 5 // maximum Buffer Queue 13 int main (void) the Ethernet socket address structure below 14 {15 16/* is very important */17 struct sockaddr_in server_addr, client_addr; 18 int Sin_size, recvbytes, wbytes; 19 int ser_fd, cli_fd; 20 char buf [BUFFER_SIZE]; 21/* establish socket connection, IPv4 protocol, byte stream socket */22 if (ser_fd = socket (AF_INET, SOCK_STREAM, 0) =-1) 23 {24 perror ("socket "); 25 exit (1); 26} 27 printf ("Socket id = % d \ n", ser_fd); 28/* initialize the sockaddr_in struct */29 server_addr.sin_family = AF_INET; 30 server_addr.sin_port = htons (PORT); 31 server_addr.sin_addr.s_addr = INADDR_ANY; 32 bzero (& (server _ Addr. sin_zero), 8); 33/* bind function */34 if (bind (ser_fd, (struct sockaddr *) & server_addr, sizeof (struct 35 sockaddr )) =-1) 36 {37 perror ("bind"); 38 exit (1); 39} 40 printf ("Bind success! \ N "); 41 42/* call the listen function to listen */43 if (listen (ser_fd, MAX_QUE_CONN_NM) =-1) 44 {45 perror ("listen"); 46 exit (1); 47} 48 printf ("Listening ...... \ n "); 49 50/* call the accept function, wait for the client to connect */51 if (cli_fd = accept (ser_fd, (struct sockaddr *) & client_addr, & sin_size) =-1) 52 {53 perror ("accept"); 54 exit (1 ); 55} 56 printf ("Have client ready for connecting \ n"); 57 58/* call the recv function to receive client requests */59 memset (buf, 0, sizeof (buf); 60 if (recvbytes = recv (cli_fd, buf, BUFFER_SIZE, 0) =-1) 61 {62 perror ("recv "); 63 exit (1); 64} 65/* print the Received information (from the client) */66 printf ("Received a message: % s \ n ", buf); 67/* processes the data sent from the client, simply adding 2 to the first character and then sending it to the client */68 buf [0] = buf [0] + 2; 69 if (wbytes = write (cli_fd, buf, strlen (buf) =-1) 70 {71 perror ("handle send"); 72 exit (1 ); 73} 74 else 75 printf ("handle buf is % s \ n", buf); 76/* close socket */77 close (ser_fd); 78 return 0; 79}
TCP client program
1/* client */2 # include <sys/types. h> 3 # include <sys/socket. h> 4 # include <sys/ioctl. h> 5 # include <netinet/in. h> 6 # include <netdb. h> 7 # include <stdio. h> 8 # include <stdlib. h> 9 # include <string. h> 10 # include <unistd. h> 11 # define PORT 2000 // PORT number 12 # define BUFFER_SIZE 102413 int main (int argc, char * argv []) 14 {15 struct sockaddr_in server_addr; 16 int sockfd, sendbytes, rbytes; 17 // int ser_fd; 18 char buf [BUFF ER_SIZE]; 19 struct hostent * host; 20/* specify three input parameters; otherwise, an error occurs */21 if (argc! = 3) 22 {23 perror ("Usage :. /clinet IP address Text \ n "); 24 exit (1); 25} 26/* address resolution function */27 if (host = gethostbyname (argv [1]) = NULL) // obtain the Host Name and corresponding information 28 {29 perror ("gethostbyname"); 30 exit (1); 31} 32 memset (buf, 0, sizeof (buf); 33 sprintf (buf, "% s", argv [2]); 34 buf [strlen (buf) + 1] = '\ 0 '; 35/* establish socket connection, IPv4 protocol, byte stream socket */36 37 if (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1) 38 {39 perror ("socket"); 40 exit (1 ); 41} 42 printf ("Socket id = % d \ n", sockfd); 43 44/* initialize the addr_in struct */45 server_addr.sin_family = AF_INET; // TCP/IP protocol cluster 46 server_addr.sin_port = htons (PORT); // sin_port storage PORT number (in byte network order) htons converts unsigned short from host byte to network byte order 47 server_addr.sin_addr = * (struct in_addr *) host-> h_addr); // previously referred, host name and pointer of the corresponding information. Here, the IP address is 48 bzero (& (server_addr.sin_zero), 8); // equivalent to memset, clearing function 49/* call the connect function to actively initiate a connection to the server */50 if (( Connect (sockfd, (struct sockaddr *) & server_addr, sizeof (struct sockaddr) =-1) // The first 2nd parameters, pointer 51 {52 perror ("connect"); 53 exit (1); 54} 55 printf ("connect server success! \ N "); 56/* send a message to the server */57 if (sendbytes = send (sockfd, buf, strlen (buf), 0) =-1) 58 {59 perror ("send"); 60 exit (1); 61} 62 else printf ("buf is % s \ n", buf ); 63/* read the processed data from the server */64 if (rbytes = read (sockfd, buf, 100) =-1) 65 {66 printf ("read handle error \ n"); 67 exit (0); 68} 69 else 70 printf ("read handle buf is % s \ n ", buf); 71 close (sockfd); 72 return 0; 73}
4. The program running result is as follows:
Client result
Server results
V. C # socket () Programming
References: http://lanxicy.com/read/9740d8d480de02ad528ada5d.html
Modify the document to generate the exe file.
The running result is as follows:
C # TCP client
C # TCP Server
6. Because both linux and C # use TCP network programming, linux is used as the client, C # is used as the server, and the port number and IP address are set,
They can communicate with each other, and then write the relevant data into the database under C #. Then, the relevant webpage can read the relevant data from the database.
The running result is as follows:
Linux Client
C # Server
VII. Summary
Network Programming in linux and C # network programming in Windows both use TCP protocol for communication, so even cross-platform communication can still be performed between them.