Linux Server-prototype of Client Communication

Source: Internet
Author: User

Server program compiled in Linux

# Include
# Include
# Include
# Include

# Define maxline 100
# Define SA struct sockaddr
# Define socket int

Using namespace STD;

Int main ()
{
Cout <"this is a server! "<Endl;
Struct sockaddr_in server, client;

Socket listen_sock = socket (af_inet, sock_stream, 0); // create a listener socket
If (listen_sock <0)
{
Perror ("socket error ");
Return-1;
}

Memset (char *) & server, 0, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (8888 );
If (BIND (listen_sock, (Sa *) & server, sizeof (server) <0) // bind the listening port
{
Perror ("BIND error ");
Return-1;
}

Listen (listen_sock, 5); // listen to sockfd a backlog queue bound to an unconnected socket Descriptor
 
Socklen_t n = (socklen_t) sizeof (client );
Socket conn_sock = (socket) accept (listen_sock, (Sa *) & client, & N); // create a connection socket
If (conn_sock <0)
Perror ("Accept error ");
Else
{
// Accept client request information
Unsigned char Buf [maxline + 1];
Memset (BUF, 0, sizeof (BUF ));
If (n = Recv (conn_sock, Buf, maxline, 0) <0)
Perror ("Recv error ");
Else
{
Buf [N] = 0;
Cout <"Recv from client:" <Buf <Endl;
}
// Server processing service ///
//////
////////////////////

// Return the server processing result string
If (send (conn_sock, "server! ", 7, 0) <0)
Perror ("send error ");
 
Close (conn_sock );
}
Close (listen_sock );
Return 0;
}

Client Program

# Include
# Include
# Include
# Include
# Include

# Define maxline 100
# Define SA struct sockaddr
# Define socket int

Using namespace STD;

Const char * IP = {"127.0.0.1 "};
Const int Port = 8888;

Int main ()
{
Cout <"this is a client! "<Endl;
Cout <"conn Server IP:" <IP <Endl;
Cout <"conn server port:" <port <Endl;

Socket sockfd;
If (sockfd = socket (af_inet, sock_stream, 0) <0)
{// Create a socket, TCP protocol, stream socket, and communication protocol
Perror ("socket error ");
Return-1;
}
Struct sockaddr_in servaddr; // communication address type variable
Memset (& servaddr, 0, sizeof (servaddr ));
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (port );
Struct hostent * HP;
HP = gethostbyname (IP );
Memcpy (char *) & servaddr. sin_addr, (char *) HP-> h_addr, HP-> h_length );

If (connect (sockfd, (Sa *) & servaddr, sizeof (servaddr) <0) // establish a connection
{
Perror ("Connect error ");
Return-1;
}

Cout <"you:" <Endl;
String message;
Cin> message;
If (send (sockfd, message. c_str (), message. Size (), 0) <0) // send the request string
{
Perror ("send error ");
Return-1;
}

Int N;
Char recvline [maxline + 1];
While (n = Recv (sockfd, recvline, maxline, 0)> 0) // accept, blocking
{
Recvline [N] = 0;
Cout <"Recv from server:" <recvline <Endl;
}
If (n <0)
Perror ("Recv error ");

Close (sockfd); // close the socket connection

Return 0;
}

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.