Linux socket programming

Source: Internet
Author: User
Linux socket programming is urgent-Linux general technology-Linux technology and application information. The following is a detailed description. // This is network socket programming
// This is the server side
# Include
# Include
# Include
# Include
# Include
# Include
# Define oops (ch) {perror (ch); return 0 ;}

Int main ()
{Int server_sockfd, client_sockfd;
Int server_len, client_len;
Struct sockaddr_un server_address;
Struct sockaddr_un client_address;

Unlink ("/tmp/server_socket ");

If (server_sockfd = socket (PF_UNIX, SOCK_STREAM, 0) =-1)
Oops ("socket ")

Server_address.sun_family = AF_UNIX;
Strcpy (server_address.sun_path, "/tmp/server_socket ");
Server_len = sizeof (server_address );

If (bind (server_sockfd, (struct sockaddr *) & server_address, server_len) =-1)
Oops ("bind ");

If (listen (server_sockfd, 4) =-1)
Oops ("listen ");

While (1)
{
Char ch;
Printf ("server waiting... \ n ");

If (client_sockfd = accept (server_sockfd, (struct sockaddr *) & client_address, & client_len) =-1)
Oops ("accept ");

Read (client_sockfd, & ch, 1 );

Ch ++;
Write (client_sockfd, & ch, 1 );
}
Return 0;

Close (client_sockfd );

}

// This is the client
# Include
# Include
# Include
# Include
# Include
# Include
# Define oops (ch) {perror (ch); return 0 ;}

Int main ()
{Int sockfd;
Int len;
Char ch = 'a ';
Struct sockaddr_un address;
If (sockfd = socket (PF_UNIX, SOCK_STREAM, 0) =-1)
Oops ("socket ");

Address. sun_family = AF_UNIX;
If (strncpy (address. sun_path, "/tmp/server_socket", 18) =-1)
Oops ("strcpy ");
Len = sizeof (address );

// If (bind (sockfd, address, len) =-1)
// Oops ("bind ");

If (connect (sockfd, (struct sockaddr *) & address, len) =-1)
Oops ("connect ");

If (write (sockfd, & ch, 1) =-1) // The program seems to be blocked here. Why !?
Oops ("write ");
Sleep (2 );
Read (sockfd, & ch, 1 );
Printf ("char from server = % c \ n", ch );
Close (sockfd );
Return 0;

}

I don't understand how to run the client after running the server.
Why does the program block write when running the client ??
Urgent!
Thank you!

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.