Linux Network Programming-general Linux technology-Linux technology and application information. For more information, see the following section. /**
* A sample server
*/
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define PORT 8888
# Define BACKLOG 2
Void process_conn_server (int sock );
Int main (int argc, char ** argv)
{
Int sSock, cSock;
Struct sockaddr_in server_addr;
Struct sockaddr_in client_addr;
Int err;
Pid_t pid;
SSock = socket (AF_INET, SOCK_STREAM, 0); // create a TCP socket
If (-1 = sSock)
{
Printf ("Create socket error ");
Return-1;
}
Bzero (& server_addr, sizeof (server_addr); // initialize the address space
Server_addr.sin_family = AF_INET;
Server_addr.sin_addr.s_addr = htonl (INADDR_ANY );
Server_addr.sin_port = htons (PORT );
Err = bind (sSock, (struct sockaddr *) & server_addr, sizeof (server_addr ));
If (err <0)
{
Printf ("bind error ");
Return-1;
}
Printf ("Bind program is right, it return % d \ n", err );
Err = listen (sSock, BACKLOG );
If (err <0)
{
Printf ("listen error ");
Return-1;
}
Printf ("Listen program is right, it return % d \ n", err );
For (;;)
{
// Int addrlen = sizeof (struct sockaddr );
Socklen_t addrlen = sizeof (struct sockaddr );
CSock = accept (sSock, (struct sockaddr *) & client_addr, & addrlen );
Perror ("The state is :");
Pid = fork ();
If (pid <0)
{
Printf ("can't create new pid ");
Return-1;
}
If (pid = 0)
{
Close (sSock );
Process_conn_server (cSock );
Exit (0 );
}
Else
Close (cSock );
}
// Return 0;
}
Void process_conn_server (int sock)
{
Ssize_t size = 0;
Char buffer [1024];
Char sendbuffer [25] = "Welcome to this server \ n ";
Write (sock, sendbuffer, 25 );
For (;;)
{
Size = read (sock, buffer, 1024); // when the byte bit 0 is read, that is, when the sender stops sending, it will return
If (size = 0)
Return;
// Sprintf (buffer, "% d bytes altogether \ n", (int) size );
Write (sock, buffer, strlen (buffer) + 1 );
Write (STDOUT_FILENO, buffer, strlen (buffer) + 1 );
}
}
/*
* Client. c
* A sample client for server
*/
# Include
// # Include
# Include
# Include
// # Include
// # Include
# Include
# Include
# Include
# Define PORT 8888
Void process_conn_client (int s );
Int main (int argc, char * argv [])
{
Int s;
If (argc! = 2)
{
Printf ("client \ N ");
Return-1;
}
Struct sockaddr_in server_addr;
S = socket (AF_INET, SOCK_STREAM, 0 );
If (s <0)
{
Printf ("failed to creating socket ");
Return-1;
}
Bzero (& server_addr, sizeof (server_addr ));
Server_addr.sin_family = AF_INET;
Server_addr.sin_port = htons (PORT );
Inet_ton (AF_INET, argv [1], & server_addr.sin_addr );
Int cnum = connect (s, (struct sockaddr *) & server_addr, sizeof (struct sockaddr); // if the call succeeds, 0 is returned; otherwise,-1 is returned.
If (cnum <0)
{
Printf ("can't find the servers \ n ");
Return 0;
}
Process_conn_client (s );
Exit (0 );
}
Void process_conn_client (int s)
{
Ssize_t size = 0;
Char buffer [1024];
Char recvbuffer [25];
Read (s, recvbuffer, 25 );
Write (STDOUT_FILENO, recvbuffer, strlen (recvbuffer) + 1 );
For (;;)
{
// Buffer [1024] = {''};
Size = read (0, buffer, 1024); // read the standard input to the buffer. Here, An Initialization is required.
If (size> 0)
Write (s, buffer, strlen (buffer) + 1 );
// Printf (buffer );
Write (STDOUT_FILENO, buffer, strlen (buffer) + 1 );
}
}
The server and client code are as shown above. When the two are connected, the client is disconnected and should have been broken up four times, but I only caught three packets with wireshark, solve it for everyone!
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