C language implements Scoket communication.

Source: Internet
Author: User

Server-side:

#include <sys/types.h> #include <sys/socket.h>                                                                         Include socket function library # include <stdio.h> #include <netinet/in.h>                                                                          Contains AF_INET related structures # include <arpa/inet.h>                                                            Functions that contain af_inet related operations # include <unistd.h>int main () {int server_sockfd, CLIENT_SOCKFD;                                                                  Used to save server and client socket identifiers int Server_len, Client_len;                                                   Used to save server and customer message length struct sockaddr_in server_address;                                                   Define a server socket address struct sockaddr_in client_address;                     Define the client socket address SERVER_SOCKFD = socket (af_inet, sock_stream, 0);          Defines socket type server_address.sin_family = af_inet;                                       Defines the domain in the socket address server_address.sin_addr.s_addr = inet_addr ("127.0.0.1");                                                              Define socket address Server_address.sin_port = 9734;   Define socket Port Server_len = sizeof (server_address);        Bind (SERVER_SOCKFD, (struct sockaddr *) &server_address, Server_len);                                                                    Defines the socket name listen (SERVER_SOCKFD, 5);      Create socket queue while (1) {char ch;      printf ("Server waits for message \ n");      Client_len = sizeof (client_address); CLIENT_SOCKFD = Accept (SERVER_SOCKFD,//Accept connection (struct sockaddr *) &      Amp;client_address, (socklen_t *__restrict) &client_len);                                                              Read (CLIENT_SOCKFD, &ch, 1);      Read customer message ch++;                                                          Write (CLIENT_SOCKFD, &ch, 1);   Send message to Customer close (CLIENT_SOCKFD); Close Connection}}


Client:

#include <sys/types.h> #include <sys/socket.h>                                                                         Include socket function library # include <stdio.h> #include <netinet/in.h>                                                                          Contains AF_INET related structures # include <arpa/inet.h>                                                                                          Functions that contain af_inet related operations # include <unistd.h>int main () {int sockfd;                                                                                                     Used to save the client socket identifier int len;                                                                  For customer message length struct sockaddr_in address;   Defines the client socket address int result;                                                                                       char ch = ' A ';                                     Defines the message to be transmitted SOCKFD = socket (af_inet,sock_stream, 0);Defines socket type address.sin_family = af_inet;                            Defines the domain in the socket address address.sin_addr.s_addr = inet_addr ("127.0.0.1");                                                                     Define socket address Address.sin_port = 9734;   Define socket Port Len = sizeof (address);                 result = Connect (SOCKFD, (struct sockaddr *) &address, Len);      Request Connection if (result = =-1) {perror ("Connection failed");   return 1;                                                                               } write (SOCKFD, &ch, 1);                                                                                Sending messages to the server read (SOCKFD, &ch, 1);   Receive messages from the server printf ("Message from the server is%c\n", ch);                                                                                       Close (SOCKFD); Close connection return 0;}



C language implements Scoket communication.

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.