TCP multithreaded Server and client program
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 Char Buf[buf
SIZE+1];
void* Fun (void* x) {//printf ("enter thread!\r\n");
int new_fd=* ((int*) x); while (1) {int z=read (new_fd,buf,bufsize);//step 6th reads socket if (z==0) {printf ("Client close!");
break;};
Buf[z]= ' ";
printf ("%s\r\n", buf);//print};
int newfd[512];
int inewfd=0;
int main () {//step 1th creates socket int sockfd=socket (af_inet,sock_stream,0);
The 2nd step is to set the address structure body struct sockaddr_in svraddr;
svraddr.sin_family=af_inet;//uses Internet Protocol svraddr.sin_port=htons (port);
Inet_aton ("0.0.0.0", &svraddr.sin_addr);
Step 3rd Binding int Ret=bind (SOCKFD, (struct sockaddr*) &svraddr,sizeof (SVRADDR));
if (ret<0) {printf ("error bind!\r\n"); exit (-1);};
4th Step Monitoring Listen (sockfd,128); while (1) {newfd[inewfd++]=accept (Sockfd,null,nulL);
The 5th step receives pthread_t Ntid;
Pthread_create (&ntid,null,fun, (void*) & (Newfd[inewfd-1));
}
}
Attention:
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
Char buf[bufsize+1];
int main ()
{
//step 1th creates an individual socket
int sockfd=socket (af_inet,sock_stream,0);
The 2nd step is to set up the addr structure body
struct sockaddr_in svraddr;
svraddr.sin_family=af_inet;//uses Internet Protocol
svraddr.sin_port=htons (port);
Inet_aton ("127.0.0.1", &svraddr.sin_addr);
The 3rd step connects server Connect
(SOCKFD, (struct sockaddr*) &svraddr,sizeof (svraddr));
while (1)
{
scanf ("%s", buf);
Write (Sockfd,buf,strlen (BUF)); Step 4th writes the string to a socket
}
}
server programs and client programs for UDP
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
Char buf[bufsize+1];
int main ()
{
//step 1th creates socket
int sockfd=socket (af_inet,sock_dgram,0);
The 2nd step is to set the address structure body
struct sockaddr_in svraddr;
svraddr.sin_family=af_inet;//uses Internet Protocol
svraddr.sin_port=htons (port);
Inet_aton ("0.0.0.0", &svraddr.sin_addr);
Step 3rd 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);//6th step read socket
buf[z]= ' "";
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
Char buf[bufsize+1];
int main ()
{
//step 1th creates an individual socket
int sockfd=socket (af_inet,sock_dgram,0);
The 2nd step is to set up the addr structure body
struct sockaddr_in svraddr;
svraddr.sin_family=af_inet;//uses Internet Protocol
svraddr.sin_port=htons (port);
Inet_aton ("127.0.0.1", &svraddr.sin_addr);
The 3rd step connects
the server//connect (SOCKFD, (struct sockaddr*) &svraddr,sizeof (svraddr));
while (1)
{
scanf ("%s", buf);
SendTo (Sockfd,buf,strlen (BUF), 0, (struct sockaddr*) &svraddr,sizeof (SVRADDR)); Step 4th writes the string to a socket
}
}