Experiment with socket for point-to-point communication under Linux (server to client)

Source: Internet
Author: User
Tags stdin strcmp server port htons

The server listens to two ports, which are then connected by the client, and no end has a send thread and accept thread dedicated to sending and receiving

Service-side process: Create a socket, bind port (to populate the network address structure first), listen for port,accept connections, and then transfer data

Client process: Create sockets, populate server-side network addresses, connect server-side ports, and transfer data


Service-side code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>


#define BUFLEN 1000
#define SEND_PORT 4000
#define Recv_port 4001
#define LISTEN_NUM 10


Set an connection, SOCKFD are the FD of sockets, CONFD is the FD of one connetction
void Set_conn (int *sockfd, int *confd,int port)
{
struct sockaddr_in s_addr, c_addr;
Socklen_t Len;
/* Build socket*/
if ((*SOCKFD = socket (af_inet, sock_stream, 0)) = = = 1)
{
Perror ("socket");
Exit (errno);
}


Fill the Address
memset (&s_addr,0,sizeof (S_ADDR));
s_addr.sin_family = af_inet;
S_addr.sin_port = htons (port);
S_ADDR.SIN_ADDR.S_ADDR = Inaddr_any;


Set the port as a reuseable port
int opt = 1;
SetSockOpt (*sockfd,sol_socket, so_reuseaddr, &opt, sizeof (opt));


Bind
if (bind (*SOCKFD, struct sockaddr*) &s_addr,sizeof (struct sockaddr)) = =-1)
{
Perror ("bind");
Exit (errno);
}


Listen
if (listen (*sockfd,listen_num) = =-1)
{
Perror ("Listen");
Exit (errno);
}


Accept
len = sizeof (struct sockaddr);
if (*CONFD = Accept (*SOCKFD, (struct sockaddr*) &c_addr, &len)) = =-1)
{
Perror ("accept");
Exit (errno);
}
}

Send Thread
void* th_send (void* Arg)
{
int SOCKFD, CONFD;
Char Buf[buflen];
Set_conn (&sockfd,&confd,send_port);
while (1)
{
Fgets (Buf,buflen,stdin);
if (!strcmp (buf, "exit"))
{
printf ("exiting\n");
Close (SOCKFD);
Close (CONFD);
return 0;
}
if (Send (Confd,buf,strlen (BUF), 0) = =-1)
{
Perror ("message sent Failed");
Exit (errno);
}
}
}

Receive thread
void* th_recv (void* Arg)
{
int SOCKFD, CONFD;
Char Buf[buflen];
Set_conn (&sockfd,&confd,recv_port);
while (1)
{
memset (Buf,0,buflen);
Switch (recv (confd,buf,buflen,0))
{
Case-1:///error occurred
Perror ("Failed to accept message");
Exit (errno);
Case 0:///peer have performed an orderly shutdown.
printf ("Client exited, chat terminated");
Return
Default://Receive succssed
printf ("client:%s", buf);
}
}
}


int main ()
{
pthread_t Tid_send,tid_recv;

Pthread_create (&tid_send,null,th_send,null);///Send Thread
Pthread_create (&tid_recv,null,th_recv,null);///Receive Thread


Pthread_join (Tid_send,null);
Pthread_join (Tid_recv,null);
return 0;
}

Client code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>


#define BUFLEN 10000
#define Send_port 4001
#define RECV_PORT 4000


int get_conn (int port)
{
int sockfd;
struct sockaddr_in s_addr;
Socklen_t Len;


/* Build socket*/
if ((SOCKFD = socket (af_inet, sock_stream, 0)) = = = 1)
{
Perror ("socket");
Exit (errno);
}


/* Set the server port */
memset (&s_addr,0,sizeof (S_ADDR));
s_addr.sin_family = af_inet;
S_addr.sin_port = htons (port);
S_ADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");


/* Connect to Server */
if (Connect (sockfd, struct sockaddr*) &s_addr,sizeof (struct sockaddr)) = =-1)
{
Perror ("Connect");
Exit (errno);
}
return SOCKFD;
}


void* th_send (void* Arg)
{


Char Buf[buflen];
int CONFD;
int ret;
CONFD = Get_conn (Send_port);
while (1)
{
Fgets (Buf,buflen,stdin);
if (!strcmp (buf, "exit"))
{
Close (CONFD);
printf ("exiting\n");
Return
}
if (Send (Confd,buf,strlen (BUF), 0) = =-1)
{
printf ("Return%d\n", ret);
Perror ("message sent Failed");
Exit (errno);
}
}
}


void* th_recv (void* Arg)
{
Char Buf[buflen];
int CONFD;
CONFD = Get_conn (Recv_port);
while (1)
{
memset (Buf,0,buflen);
Switch (recv (confd,buf,buflen,0))
{
Case-1:///error occurred
Perror ("Failed to accept message");
Exit (errno);
Case 0:///peer have performed an orderly shutdown.
printf ("Server exited, chat terminated");
Return
Default://Receive succssed
printf ("server:%s", buf);
}
}
}


int main ()
{
pthread_t Tid_send;
pthread_t Tid_recv;

Pthread_create (&tid_send,null,th_send,null);
Pthread_create (&tid_recv,null,th_recv,null);

Pthread_join (Tid_send,null);
Pthread_join (Tid_recv,null);
return 0;
}

Experiment with socket for point-to-point communication under Linux (server to client)

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.