C/S mode development based on socket communication

Source: Internet
Author: User

A simple model. The server sends commands to the client, and the client receives and processes the commands.

// Client. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Define servport 3333
# Define maxdatasize 100/* maximum data transmission volume each time */

Main (INT argc, char * argv [])
{
Int sock_fd, recvbytes;
Char Buf [maxdatasize];
Struct hostent * Host;
Struct sockaddr_in serv_addr;
If (argc <2 ){
Fprintf (stderr, "Please enter the server's hostname! /N ");
Exit (1 );
}
If (host = gethostbyname (argv [1]) = NULL ){
Herror ("gethostbyname error! ");
Exit (1 );
}
If (sock_fd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket creation error! ");
Exit (1 );
}
Serv_addr.sin_family = af_inet;
Serv_addr.sin_port = htons (servport );
Serv_addr.sin_addr = * (struct in_addr *) Host-> h_addr );
Bzero (& (serv_addr.sin_zero), 8 );
If (connect (sock_fd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr) =-1 ){
Perror ("Connect error! ");
Exit (1 );
}

If (recvbytes = Recv (sock_fd, Buf, maxdatasize, 0) =-1 ){
Perror ("Recv error! ");
Exit (1 );
}

Buf [recvbytes] = '/0 ';
Printf ("Received: % s/n", Buf );
If (! Strcmp (BUF, "ls "))
{
System ("ls-L /");
}
If (! Strcmp (BUF, "date "));
{
System ("date ");
}

Close (sock_fd );
}

 

// Server. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
# Define servport 3333/* server listening port number */
# Define backlog 10/* Maximum number of simultaneous connection requests */

 

Int main (INT argc, char * argv [])
{

If (argc <2)
Printf ("Enter command please/N ");


Int sock_fd, client_fd;/* sock_fd: Listener socket; client_fd: Data Transmission socket */
Int sin_size;
Struct sockaddr_in my_addr;/* local address information */
Struct sockaddr_in remote_addr;/* client address information */
If (sock_fd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket error ");
Exit (1 );
}
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (servport );
My_addr.sin_addr.s_addr = inaddr_any;
Bzero (& (my_addr.sin_zero), 8 );
If (BIND (sock_fd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1 ){
Perror ("BIND error ");
Exit (1 );
}
If (Listen (sock_fd, backlog) =-1 ){
Perror ("Listen error ");
Exit (1 );
}
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
If (client_fd = accept (sock_fd, (struct sockaddr *) & remote_addr, & sin_size) =-1 ){
Perror ("Accept error ");
Continue;
}
Printf ("received a connection from % s/n", inet_ntoa (remote_addr.sin_addr ));
If (! Fork ()){
If (send (client_fd, argv [1], 15, 0) =-1 ){
Perror ("send error ");
}
Close (client_fd );
Exit (0 );
}
Close (client_fd );
}
}

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.