linux socket编程 udp connect例子

 

#include <stdarg.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/select.h>
#include <math.h>

void process_client(int s) {

     char buff[128];
     int rc,maxfd;
     while(fgets(buff,sizeof(buff),stdin)!=NULL) {
      write(s,buff,strlen(buff));
      rc = read(s,buff,sizeof(buff));
      if(rc <= 0)
         error(1,0,"no data!!/n");
 buff[rc]='/0';
 write(1,buff,rc);
 bzero(buff,sizeof(buff));
     }
    
}

int main(int argc, char **argv) {
    struct sockaddr_in server,temp;
    char *hname, *sname;
    int s, c, len;
    if(argc < 2)
     error(1,0,"parameters less than 2/n");
    else if(argc == 2) {
     hname = NULL;
 sname = argv[1];
    }
    else if(argc == 3) {
     hname = argv[1];
 sname = argv[2];
    }
   
    s = client_connect(hname, sname, &server, "udp",1);
    printf("%s,%d",inet_ntoa(server.sin_addr),ntohs(server.sin_port));
    process_client(s);
    close(s);
    exit(0);
}
 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页