A simple example of socket communication between two programs in Linux

Source: Internet
Author: User

Note:

These two programs should be run on the same machine. You need to create the/home/rangaofei/c_test/directory in advance.

This afternoon I learned about socket communication programming, read a book, and find an online explanation. Although it is still dizzy, the small experiment is still successful, and I plan to study it further tomorrow.

Two simple small programs, client. C and server. C, are compiled in the Ubuntu environment. One serves as the Socket Client and the other serves as the socket server. The GCC compilation is successful, and the communication is successful. Paste the code below:


/****************** Server program *****************/

# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <string. h>
# Include <stdlib. h>
# Include <sys/UN. h>

Int main (){
Int sockfd, newfd, RET, recv_num, recv_num_total = 0;
Char Buf [50];
Struct sockaddr_un server_addr;
Remove ("/home/rangaofei/c_test/Server ");
Memset (& server_addr, 0, sizeof (server_addr ));
Server_addr.sun_family = af_unix;
Strcpy (server_addr.sun_path, "/home/rangaofei/c_test/Server ");
Sockfd = socket (af_unix, sock_stream, 0 );
If (sockfd <0 ){
Printf ("INVOKE socket function to build socket handle error! \ N ");
Exit (1 );
}
Printf ("build socket handle success ~ \ N ");
Ret = BIND (sockfd, (struct sockaddr *) (& server_addr), sizeof (server_addr ));
If (Ret <0 ){
Printf ("INVOKE bind func bind socket handle and address error! \ N ");
Exit (2 );
}
Printf ("bind socket handle and address specficed Success ~ \ N ");
Ret = listen (sockfd, 4 );
If (Ret <0 ){
Printf ("INVOKE listen function to make sure server can respond the request error! \ N ");
Exit (3 );
}
Printf ("respond the request Success ~ \ N ");
Newfd = accept (sockfd, null, null); // newfd connect client which call connect function
If (newfd <0 ){
Printf ("Call accept function error, build connection failed! \ N ");
Exit (4 );
}
Printf ("build connection with client success ~ \ N ");
While (1 ){
Recv_num = Recv (newfd, Buf, 24, 0 );
If (recv_num <0)
Printf ("Call Recv function receive message failed! \ N ");
Else {
Recv_num_total + = recv_num;
Printf ("Call Recv seccess ~, Received % d Words, message is \ "% s \". Total date received is % d. \ n ", recv_num, Buf, recv_num_total );
}
Sleep (1 );
}
}



/****************** Client program *****************/

# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <string. h>
# Include <stdlib. h>
# Include <sys/UN. h>

Int main (){
Int sockfd, RET, send_num, send_num_total = 0;
Char Buf [50] = "this is my socket data .";
Struct sockaddr_un server_addr;
Memset (& server_addr, 0, sizeof (server_addr ));
Server_addr.sun_family = af_unix;
Strcpy (server_addr.sun_path, "/home/rangaofei/c_test/Server ");
Sockfd = socket (af_unix, sock_stream, 0 );
If (sockfd <0 ){
Printf ("INVOKE socket function to build socket handle error! \ N ");
Exit (1 );
}
Printf ("build socket handle success ~ \ N ");
Ret = connect (sockfd, (struct sockaddr *) (& server_addr), sizeof (server_addr ));
If (Ret <0 ){
Printf ("INVOKE connect func to connect server faild! \ N ");
Exit (2 );
}
Printf ("Connect server Success ~ \ N ");
While (1 ){
Send_num = Send (sockfd, Buf, sizeof (BUF), msg_dontwait );
If (send_num <0)
Printf ("call send function to send message to server failed! \ N ");
Else {
Send_num_total + = send_num;
Printf ("call send func seccess ~, Send % d Words, message is \ "% s \". Total date send is % d. \ n ", send_num, Buf, send_num_total );
}
Sleep (1 );
}
}


Note: When running these two programs, you must first run the server-side program; otherwise, an error will occur. The client cannot find the server. After the server is running, it will be blocked to wait for the client connection.

Reference: http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

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.