Linux socket chat program (process (fork) Version)

Source: Internet
Author: User

 

 

/**************************** Client. c ***********************************/

 

# Include <stdio. h>
# Include <unistd. h>
# Include <strings. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <netdb. h>

# Define Port 1234/* Listen port */
# Define maxdatasalize 100

Int main (INT argc, char * argv [])
{
Int sockfd, num;
Pid_t PID;
Char Buf [maxdatasize];
Struct hostent * He;
Struct sockaddr_in server;

If (argc! = 2)
{
Printf ("usage % S <IP address>/N", argv [0]);
Exit (1 );
}
/* Get IP Address */
If (He = gethostbyname (argv [1]) = NULL)
{
Printf ("gethostbyname error/N ");
Exit (1 );
}
If (sockfd = socket (af_inet, sock_stream, 0) =-1)
{
Printf ("socket () Error/N ");
Exit (1 );
}

Bzero (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (port );
Server. sin_addr = * (struct in_addr *) He-> h_addr );

If (connect (sockfd, (struct sockaddr *) & server, sizeof (server) =-1)
{
Printf ("connetc () Error/N ");
Exit (1 );
}
If (pid = fork ()> 0)
{
While (1)
{
Fgets (BUF, maxdatasize, stdin );
Send (sockfd, Buf, maxdatasize, 0 );

}
}
Else if (pid = 0)
{While (1)
{
Recv (sockfd, Buf, maxdatasize, 0 );
Printf ("Server Message: % s", Buf );
}

}
Else
Return 1;
 
Close (sockfd );
}

 

 

 

 

 

 

 

/*************************************** ****************/

/* Server. C */

/*************************************** ***************/

 

# Include <stdio. h>
# Include <strings. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>

# Define Port 1234
# Define backlog 1
# Define maxdatasalize 100

Void main ()
{
Int listenfd, connectfd;
Struct sockaddr_in server;
Struct sockaddr_in client;
Socklen_t addrlen;
Char Buf [maxdatasize];
Pid_t PID;
 
If (listenfd = socket (af_inet, sock_stream, 0) =-1)
{
Perror ("socket () error .");
Exit (1 );
}
 
Int opt = so_reuseaddr;
Setsockopt (listenfd, sol_socket, so_reuseaddr, & OPT, sizeof (OPT ));
 
Bzero (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (port );
Server. sin_addr.s_addr = htonl (inaddr_any );
If (BIND (listenfd, (struct sockaddr *) & server, sizeof (server) =-1)
{
Perror ("BIND () error .");
Exit (1 );
}
If (Listen (listenfd, backlog) =-1)
{
Perror ("Listen () error .");
Exit (1 );
}
 
Addrlen = sizeof (client );
While (1)
{
If (connectfd = accept (listenfd, (struct sockaddr *) & client, & addrlen) =-1)
{
Perror ("accept () error .");
Exit (1 );
}
If (pid = fork ()> 0)
{
While (1)
{
Close (listenfd );
Recv (connectfd, Buf, maxdatasize, 0 );
Printf ("the client says: % s/n", Buf );
}

}
Else if (pid = 0)
{
While (1)
{
Close (listenfd );
Fgets (BUF, maxdatasize, stdin );
Send (connectfd, Buf, maxdatasize, 0 );
}
}
Else
Return 1;
}
Close (connectfd );
Close (listenfd );
}

 

 

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.