编写Linux下socket协议TCP和UDP的Client Server程序

http://wenku.baidu.com/view/0803b63610661ed9ad51f32b.html

一、编写Linux下socket协议TCP的ClientServer程序
 
客户端,client:
 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#define MAXBUF 256
int main()
{
   int ssock;
   int clen;
   struct sockaddr_in server_addr;
   char buf[MAXBUF];
   if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
   perror("socket error:");
   exit(1);
   }
   clen = sizeof(server_addr);
   memset(&server_addr,0,sizeof(server_addr));
   server_addr.sin_family     =AF_INET;
   server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
   server_addr.sin_port       =htons(7758);
   if(connect(ssock,(struct sockaddr *)&server_addr,clen)<0){
   perror("connect error:");
   exit(1);
   }
   memset(buf,0,MAXBUF);
   if(read(ssock,buf,MAXBUF)<=0)
   {
      perror("read error:");
      exit(1);
   }
   close(ssock);
   printf("\nread: %s\n",buf);
   return 0;
}
 

 
服务端,server:
 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#define MAXBUF 256
int main()
{
    int clen;
    int ssock,csock;
    struct sockaddr_in client_addr,server_addr;
    char buf[MAXBUF];
    if((ssock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
    perror("socket error:");
    exit(1);
    }
    clen = sizeof(client_addr);
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family     =AF_INET;
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 
    server_addr.sin_port=htons(7758);
    if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){
    perror("bind error");
    exit(1);
    }
    if(listen(ssock,8)<0){
    perror("listen error:");
    exit(1);
    }
    while(1){
    csock=accept(ssock,(struct sockaddr *)&client_addr,&clen);
    if(write(csock,buf, MAXBUF)<=0)
    perror("write error:");
    close(csock);
    }
    return 0;
} 

 


 
二、编写Linux下的socket协议UDP Client/Server程序
客户端,client:
 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#define MAXBUF 256
int main()
{
   int ssock;
   int clen;
   struct sockaddr_in client_addr,server_addr;
   char buf[MAXBUF];
   strcpy(buf,"I miss you already!\n");
   if((ssock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){
   perror("socket error:\n");
   exit(1);
   }
   memset(&server_addr,0,sizeof(server_addr));
   server_addr.sin_family  =AF_INET;
   server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
   server_addr.sin_port=htons(7758);
   sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,sizeof(server_addr));
   printf("Send message:%s\n",buf);
   clen=sizeof(client_addr);
   recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&server_addr,&clen);
   printf("Receive message:%s\n",buf);
   close(ssock);
   return 0;
} 

 


 
服务端,server:
 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#define MAXBUF 256
int main()
{
    int ssock;
    int clen;
    struct sockaddr_in client_addr,server_addr;
    char buf[MAXBUF];
    if((ssock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0){
    perror("socket error:");
    exit(1);
    }
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    server_addr.sin_port=htons(7758);
    if(bind(ssock,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){
    perror("bind error:");
    exit(1);
    }
    clen=sizeof(client_addr);
    recvfrom(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr,&clen);
    strcpy(buf,"I miss you");
    sendto(ssock,(void *)buf,MAXBUF,0,(struct sockaddr*)&client_addr ,sizeof(client_addr));
    close(ssock);
    return 0;
} 

 


 

转载于:https://www.cnblogs.com/Adrian99/p/3299076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值