linux-udp网络编程-c/s

1 .server

deepfuture@deepfuture-laptop:~/private/mytest$ ./testupds

server wait....

 

server read :deepfuture.iteye.com

 

server send :hello

 

server wait....

^C

deepfuture@deepfuture-laptop:~/private/mytest$ 

2.client

deepfuture@deepfuture-laptop:~/private/mytest$ ./testupdc

client send....

client send :deepfuture.iteye.com

 

client read :hello

 

deepfuture@deepfuture-laptop:~/private/mytest$ 

3.source
1)server
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//deepfuture.iteye.com

int main(void){
    int fd;
    int addresslen;
    struct sockaddr_in address;//地址信息结构

    int rc;


                  
    //建立socket
    fd=socket(AF_INET,SOCK_DGRAM,0);//fd为socket
    if (fd==-1){//错误,类型从errno获得
        perror("error");//perror先输出参数,后跟":"加空格,然后是errno值对应的错误信息(不是错误代码),最后是一个换行符。        
    }
    
    //bind 到socket fd    
    address.sin_family=AF_INET;//IPV4协议,AF_INET6是IPV6
    address.sin_addr.s_addr=htonl(INADDR_ANY);//l表示32位,htonl能保证在不同CPU的相同字节序
    address.sin_port=htons(1253);//端口号,s表示16位
    addresslen=sizeof(address);
    

    bind(fd,(struct sockaddr *)&address,addresslen);//bind
    

           while(1){

              struct sockaddr_in clientaddress;
              int address_len;

              char mybuf[100];    
              char *buf="hello\n";  
              int n;            


              printf("server wait....\n");
              fflush(stdout);     
              address_len=sizeof(clientaddress);
              n=recvfrom(fd,mybuf,100,0,(struct sockaddr *)&clientaddress,&address_len);//读数据
              printf("server read :%s\n",mybuf);    
              sendto(fd,buf,strlen(buf)+1,0,(struct sockaddr *)&clientaddress,address_len);//写数据    
              printf("server send :%s\n",buf);    
  
             }
  }
 2)client
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//deepfuture.iteye.com

int main(void){
    int fd;
    int addresslen;
    struct sockaddr_in address;//地址信息结构

    int rc;

    char mybuf[100];    
    char *buf="deepfuture.iteye.com\n";  
    int n;   


                  
    //建立socket
    fd=socket(AF_INET,SOCK_DGRAM,0);//fd为socket
    if (fd==-1){//错误,类型从errno获得
        perror("error");//perror先输出参数,后跟":"加空格,然后是errno值对应的错误信息(不是错误代码),最后是一个换行符。        
    }
    
    //bind 到socket fd    
    address.sin_family=AF_INET;//IPV4协议,AF_INET6是IPV6
    address.sin_addr.s_addr=htonl(INADDR_ANY);//l表示32位,htonl能保证在不同CPU的相同字节序
    address.sin_port=htons(1253);//端口号,s表示16位
    addresslen=sizeof(address);
    

         


              printf("client send....\n");
              fflush(stdout);     
              sendto(fd,buf,strlen(buf)+1,0,(struct sockaddr *)&address,addresslen);//写数据    
              printf("client send :%s\n",buf);     
              n=recvfrom(fd,mybuf,100,0,NULL,NULL);//读数据
              printf("client read :%s\n",mybuf);    

  

  }


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值