Transferred from: http://blog.csdn.net/kikilizhm/article/details/7858405
This gives an example of a simple socket network programming under Linux, communicates with the TCP protocol, listens on the server, sends the data to the client after receiving the client's connection, and the client prints and closes after receiving the data. There are detailed instructions in the program, in which the specific structure and functions of the implementation can refer to other information.
Program Description: Here the server port number and IP address using fixed settings, the migration can be changed according to the specific circumstances, can be rewritten as a parameter to pass better, here for convenience, use fixed.
The server can be run without changes at the time of migration, the client will change IP to the address of the servers, and then compile and run. You can use Netstat to view the appropriate run status.
[CPP]View Plaincopyprint?
- /*************************************
- File name: server.c
- A simple example of Linux socket network programming-service-side program
- The server port is set to 0x8888 (ports and addresses can be changed depending on the situation, or passed in with parameters)
- Server address set to 192.168.1.104
- Author: kikilizhm#163.com (change # to @)
- */
- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <linux/in.h>
- #include <string.h>
- int main ()
- {
- int SFP,NFP; / * Define two descriptors * /
- struct sockaddr_in s_add,c_add;
- int sin_size;
- unsigned short portnum=0x8888; / * Use port on server * /
- printf ("hello,welcome to my server!\r\n");
- SFP = socket (af_inet, sock_stream, 0);
- if ( -1 = = SFP)
- {
- printf ("socket fail! \ r \ n ");
- return-1;
- }
- printf ("socket OK!\r\n");
- /* Populate the server port address information so that the following address and port are used for monitoring */
- Bzero (&s_add,sizeof (struct sockaddr_in));
- S_add.sin_family=af_inet;
- S_add.sin_addr.s_addr=htonl (Inaddr_any); / * This address uses full 0, i.e. all * /
- S_add.sin_port=htons (Portnum);
- /* Bind port with bind */
- if ( -1 = = Bind (SFP, (struct sockaddr *) (&s_add), sizeof (struct sockaddr)))
- {
- printf ("bind fail!\r\n");
- return-1;
- }
- printf ("bind OK!\r\n");
- /* Start listening to the appropriate port */
- if ( -1 = = Listen (sfp,5))
- {
- printf ("Listen Fail!\r\n");
- return-1;
- }
- printf ("Listen ok\r\n");
- while (1)
- {
- Sin_size = sizeof (struct sockaddr_in);
- /* Accept the service side using the function, the call will enter the blocking state, waiting for the user to connect, when no client to connect, the program stopped here,
- You will not see the subsequent print, and when a client makes a connection, the program executes once and then loops back here to continue waiting.
- The second parameter of the accept here is used to obtain the port and address information for the client.
- */
- NFP = Accept (SFP,struct sockaddr *) (&c_add), &sin_size);
- if ( -1 = = NFP)
- {
- printf ("Accept fail!\r\n");
- return-1;
- }
- printf ("Accept ok!\r\nserver start get connect from% #x:% #x \ r \ n", Ntohl (C_ADD.SIN_ADDR.S_ADDR), Ntohs (c_add.sin_ Port));
- /* Use write to send a message to the client, or you can try using another function to implement */
- if ( -1 = = Write (NFP,"hello,welcome to my server \ r \ n", +))
- {
- printf ("Write fail!\r\n");
- return-1;
- }
- printf ("Write ok!\r\n");
- Close (NFP);
- }
- Close (SFP);
- return 0;
- }
[CPP]View Plaincopyprint?
- /*************************************
- File name: client.c
- Simple example of Linux socket network programming-client program
- The server port is set to 0x8888 (ports and addresses can be changed depending on the situation, or passed in with parameters)
- Server address set to 192.168.1.104
- Author: kikilizhm#163.com (change # to @)
- */
- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <linux/in.h>
- #include <string.h>
- int main ()
- {
- int CFD; / * File descriptor * /
- int recbytes;
- int sin_size;
- Char buffer[1024]={0}; / * Accept buffer * /
- struct sockaddr_in s_add,c_add; / * Storage server and local IP, port and other information structures */
- unsigned short portnum=0x8888; /* The communication port used by the server can be changed to be the same as the server */
- printf ("Hello,welcome to Client!\r\n");
- /* Build sockets using the Internet, TCP streaming */
- CFD = socket (af_inet, sock_stream, 0);
- if ( -1 = = CFD)
- {
- printf ("socket fail! \ r \ n ");
- return-1;
- }
- printf ("socket OK!\r\n");
- /* Constructs the server side IP and port information, the concrete structure may check the data * *
- Bzero (&s_add,sizeof (struct sockaddr_in));
- S_add.sin_family=af_inet;
- S_add.sin_addr.s_addr= inet_addr ("192.168.1.104"); / * IP is converted to 4-byte shaping and needs to be changed according to the server IP when using * /
- S_add.sin_port=htons (Portnum); / * Here htons is to convert the short data byte sequence from the host type to the network type, actually is
- Two bytes of 2 bytes of data are switched, and the corresponding Ntohs effect, the same substance, but the name is different. Htonl and Ntohl are
- The 4-byte shaping of the operation. Change the 0x12345678 to 0x78563412, the name is different, the content 22 is the same, in general, the network is big-endian,
- PPC CPU is big, x86 CPU for small end, arm can configure the size of the end, need to ensure that the byte sequence is correct when received.
- */
- printf ("s_addr =% #x, port:% #x \ r \ n", s_add.sin_addr.s_addr,s_add.sin_port); / * The small end is printed here
- Contrary to what we normally see. */
- /* Client Connection server, parameters are socket file descriptor, address information, address structure size */
- if ( -1 = = Connect (CfD, (struct sockaddr *) (&s_add), sizeof (struct sockaddr)))
- {
- printf ("Connect fail!\r\n");
- return-1;
- }
- printf ("Connect OK!\r\n");
- /* Connection succeeded, receive characters from server */
- if ( -1 = = (Recbytes = read (cfd,buffer,1024)))
- {
- printf ("read data fail!\r\n");
- return-1;
- }
- printf ("read ok\r\nrec:\r\n");
- buffer[recbytes]=' + ';
- printf ("%s\r\n", buffer);
- GetChar (); / * This sentence is for the program to pause here, you can use Netstat to view the current connection * /
- Close (CFD); / * Close the connection, this communication is complete * /
- return 0;
- }
Simple example of C language socket network programming under Linux