The C language is used to write an Socket communication program based on the TCP and UDP protocols
- 2020-05-09 19:01:49
- OfStack
Tcp multithreaded server and client programs
Server program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
void* fun(void* x)
{
//printf("enter thread!\r\n");
int new_fd=*((int*)x);
while(1)
{
int z=read(new_fd,buf,BUFSIZE);// The first 6 step Read the socket
if(z==0){printf("client close !");break;};
buf[z]='\0';
printf("%s\r\n",buf);// print
};
}
int newfd[512];
int inewfd=0;
int main()
{
// The first 1 step Create a socket
int sockfd=socket(AF_INET,SOCK_STREAM,0);
// The first 2 step Set the address structure
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;// use internet agreement
svraddr.sin_port=htons(PORT);
inet_aton("0.0.0.0",&svraddr.sin_addr);
// The first 3 step The binding
int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
if(ret<0){printf("error bind!\r\n");exit(-1);};
// The first 4 step Listening to the
listen(sockfd,128);
while(1)
{
newfd[inewfd++]=accept(sockfd,NULL,NULL); // The first 5 step receive
pthread_t ntid;
pthread_create(&ntid,NULL,fun,(void*)&(newfd[inewfd-1]));
}
}
Note:
gcc server.c -o server -lpthread
Client program cli.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
// The first 1 step create 1 Individual socket
int sockfd=socket(AF_INET,SOCK_STREAM,0);
// The first 2 step Set up the addr The structure of the body
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;// use internet agreement
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
// The first 3 step Connect to server
connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
write(sockfd,buf,strlen(buf)); // The first 4 step Writes a string to a socket
}
}
Udp server and client programs
Server program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
// The first 1 step Create a socket
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
// The first 2 step Set the address structure
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;// use internet agreement
svraddr.sin_port=htons(PORT);
inet_aton("0.0.0.0",&svraddr.sin_addr);
// The first 3 step The binding
int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
if(ret<0){printf("cannot bind!\r\n");exit(-1);};
while(1)
{
struct sockaddr_in cli;
int len=sizeof(cli);
int z=recvfrom(sockfd,buf,BUFSIZE,0,(struct sockaddr*)&cli,&len);// The first 6 step Read the socket
buf[z]='\0';
printf("%s\r\n",buf);// print
}
}
Client program cli.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
// The first 1 step create 1 Individual socket
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
// The first 2 step Set up the addr The structure of the body
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;// use internet agreement
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
// The first 3 step Connect to server
//connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&svraddr,sizeof(svraddr)); // The first 4 step Writes a string to a socket
}
}