Simple TCP point-to-point chat program

Source: Internet
Author: User
Tags sendmsg socket error htons
I. Question

Write a one-to-one chat program between a socket-based TCP server and a client in Linux using standard C.

Requirements:

1. Compile the server program and name it tcp_server.c.

2. Compile the client program and name it tcp_client.c.

3. Start chatting by sending the string beginchat from the client, send the string quitchat to quit chatting, and end the programs on both sides.

4. The Chat content is input on the keyboard.

Ii. Procedures

Tcp_server.c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ctype. h>

# Define server_port 3456
# Define backlog 10
# Deprecision max_size 1024

Void sendmsg (INT to_sockfd, char * MSG); // send a message
Char * recvmsg (INT from_sockfd); // receives the message

Int main ()
{
Struct sockaddr_in server_addr, client_addr;
Int addr_len = sizeof (struct sockaddr_in );

Int server_fd, client_fd;

// Initialize the addr_in struct on the server
Server_addr.sin_family = af_inet;
Server_addr.sin_port = htons (server_port );
Server_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); // OR = inaddr_any
Bzero (& (server_addr.sin_zero), 8 );

// Create a socket
Server_fd = socket (af_inet, sock_stream, 0 );
If (server_fd =-1 ){
Perror ("socket error ");
Exit (-1 );
}

// Bind a socket
Int bind_stats = BIND (server_fd, (struct sockaddr *) & server_addr, addr_len );
If (bind_stats =-1 ){
Perror ("BIND error ");
Exit (-1 );
}

// Listen
If (Listen (server_fd, backlog) =-1 ){
Perror ("Listen error ");
Exit (-1 );
}

// Wait cyclically for client connection
While (1)
{
// Obtain the connection
Client_fd = accept (server_fd, (struct sockaddr *) & client_addr, & addr_len );
If (client_fd =-1)
{
Perror ("Accept error ");
}
Printf ("server get connection from % s \ n", inet_ntoa (client_addr.sin_addr ));

Sendmsg (client_fd, "connected ");

Char chat_msg [max_size]; // storage Information
Char reply_msg [max_size]; // Response Message
Bzero (chat_msg, max_size );

/* Int byte_num = Recv (client_fd, chat_msg, max_size, 0); // obtain the number of characters received
Chat_msg [byte_num] = '\ 0'; // ending string */
Strcpy (chat_msg, recvmsg (client_fd ));

If (strcmp (chat_msg, "beginchat") = 0) // start chatting when 'beginchat' is received
{
Printf ("client begin chat \ n ");
Sendmsg (client_fd, "OK, chat begin \ n ");
// Q & A Mode
While (1)
{
Bzero (chat_msg, max_size );
Strcpy (chat_msg, recvmsg (client_fd); // receive information

If (strcmp (chat_msg, "quitchat") = 0)
{
Printf ("client logoff \ n ");
Break;
}

Printf ("client said: % s \ n", chat_msg); // displays received information
Printf ("me :");
Scanf ("% s", reply_msg); // enter the reply Information
Sendmsg (client_fd, reply_msg); // send the reply message
}
Close (client_fd );
}
}

Return 0;
}

// Message sending Function
Void sendmsg (INT to_sockfd, char * MSG)
{
Int Len = strlen (MSG );
If (send (to_sockfd, MSG, max_size, 0) =-1 ){
Perror ("send error ");
}
}

// Receives the message
Char * recvmsg (INT from_sockfd)
{
Char MSG [max_size];
Bzero (MSG, max_size );
Int byte_num = Recv (from_sockfd, MSG, max_size, 0 );
MSG [byte_num] = '\ 0 ';
Return MSG;
}

Tcp_client.c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ctype. h>

# Define dest_port 3456
# Define dest_ip "127.0.0.1"
# Deprecision max_size 1024

Void sendmsg (INT to_sockfd, char * MSG); // send a message
Char * recvmsg (INT from_sockfd); // receives the message

Int main ()
{
Struct sockaddr_in server_addr;
Int addr_len = sizeof (struct sockaddr_in );

Int server_fd, client_fd;

Server_addr.sin_family = af_inet;
Server_addr.sin_port = htons (dest_port );
Server_addr.sin_addr.s_addr = inet_addr (dest_ip );
Bzero (& (server_addr.sin_zero), 8 );

Server_fd = socket (af_inet, sock_stream, 0 );

If (connect (server_fd, (struct sockaddr *) & server_addr, addr_len) = 0)
{
Char chat_msg [max_size]; // storage Information
Bzero (chat_msg, max_size );

Strcpy (chat_msg, recvmsg (server_fd ));
If (strcmp (chat_msg, "connected") = 0)
{
Printf ("% s \ n", chat_msg );
While (1)
{
Printf ("me :");
Scanf ("% s", chat_msg); // enter the information
Sendmsg (server_fd, chat_msg); // send information
If (strcmp (chat_msg, "quitchat") = 0)
{
Printf ("endchat \ n ");
Break;
}
Bzero (chat_msg, max_size );
Strcpy (chat_msg, recvmsg (server_fd); // receives messages
Printf ("server reply: % s \ n", chat_msg );
}
}
Close (server_fd );
}

Return 0;
}

// Message sending Function
Void sendmsg (INT to_sockfd, char * MSG)
{
Int Len = strlen (MSG );
If (send (to_sockfd, MSG, max_size, 0) =-1 ){
Perror ("send error ");
}
}

// Receives the message
Char * recvmsg (INT from_sockfd)
{
Char MSG [max_size];
Bzero (MSG, max_size );
Int byte_num = Recv (from_sockfd, MSG, max_size, 0 );
MSG [byte_num] = '\ 0 ';
Return MSG;

}

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.