Multi-thread communication instance for C network programming in Linux

Source: Internet
Author: User
Tags htons

For a simple example, please refer to the following link for more information.

Server

/* ===================================================== ============================================================ Name: sockthreadserver. c Author: gaotong version: Copyright: Your copyright notice Description: Hello world in C, ANSI-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # includ E <pthread. h> # include <sys/syscall. h> # include <unistd. h> # include <string. h> # define gettid () syscall (_ nr_gettid) // obtain the thread idvoid * handler_conn (void * Arg) {char send_str [100]; int client_sock_fd = * (int *) ARG; char recv_str [100]; // printf ("thread run success \ n"); While (1) {read (client_sock_fd, recv_str, 100 ); // Disconnect if (strcmp (recv_str, "exit") = 0) {break;} printf ("receive from % d: % s \ n", Client_sock_fd, recv_str); int id = gettid (); // get the thread idsprintf (send_str, "this is thread: % d, hello, client_sock_fd: % d \ n", ID, client_sock_fd); If (write (client_sock_fd, send_str, 100) =-1) {perror ("send error"); // The client closes the break connection ;} printf ("I said: % s \ n", send_str);} Free (ARG); puts ("disconnected \ n"); close (client_sock_fd ); pthread_exit (0);} int main (void) {int * client_sockfd; int server_len, client_len; struct sockaddr_in server_add RESS; struct sockaddr_in client_address; struct sockaddr_in tempaddr; server_address.sin_family = af_inet; region = htons (1991); region = inet_addr ("127.0.0.1 "); // create a socketint sock_fd = socket (af_inet, sock_stream, 0); // BIND (sock_fd, (struct sockaddr *) & server_address, sizeof (server_address )); listen (sock_fd, 20); While (1) {pthread_t thread; // each connection to a client is created One thread puts ("listening wait :............ \ N "); // client_sockfd needs to be passed to the thread as a pointer. So that each thread can control its own connection client_sockfd = (int *) malloc (sizeof (INT); client_len = sizeof (client_address); // int accept (INT socket, struct sockaddr * ADDR, socklen_t * plugin) * client_sockfd = accept (sock_fd, (struct sockaddr *) & client_address, (socklen_t *) & client_len); printf ("Client Connected! % D \ n ", * client_sockfd); pthread_create (& Thread, null, handler_conn, client_sockfd); puts (" thread creation \ n ");} return exit_success ;}

Client:

/* ===================================================== ============================================================ Name: sockthreadclient. c Author: gaotong version: Copyright: Your copyright notice Description: Hello world in C, ANSI-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # includ E <pthread. h> # include <unistd. h> # include <string. h> int client_sockfd; char recv_str [100]; void * getmsg (void * Arg) {int test; while (test = read (client_sockfd, recv_str, 100 ))! = EOF) {If (test = 0) // The server closes the break connection; If (test =-1) {printf ("read error! "); Break;} printf (" received Server Message: % s \ n ", recv_str);} Close (client_sockfd); pthread_exit (0);} int main (void) {char char_send [100]; // create a socketclient_sockfd = socket (af_inet, sock_stream, 0); struct sockaddr_in serveraddr; // The server address serveraddr. sin_addr.s_addr = inet_addr ("127.0.0.1"); serveraddr. sin_family = af_inet; serveraddr. sin_port = htons (1991); Connect (client_sockfd, (struct sockaddr *) & serveraddr, sizeof (serveraddr); pthread_t pt; pthread_create (& PT, null, getmsg, null ); while (1) {gets (char_send); // read the data to be sent from the console // int send (INT socket, void * buffer, size_t size, int flags) if (send (client_sockfd, char_send, sizeof (char_send), 0) =-1) {break;} printf ("I said to the server: % s \ n ", char_send); If (strcmp (char_send, "exit") = 0) {// enter exit to exit break ;}} close (client_sockfd); Return exit_success ;}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.