zoukankan      html  css  js  c++  java
  • Linux基础编程之网络编程TCP实例

    //server
    #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> #include<unistd.h> #include <arpa/inet.h> #include<pthread.h> static void usage(const char *proc) { printf("Please use :%s [IP] [port] ",proc); } void thread_run(void *arg) { printf("creat a new thread "); int fd = (int)arg; char buf[1024]; while(1){ memset(buf,'',sizeof(buf)); ssize_t _s = read(fd,buf,sizeof(buf) - 1); if(_s > 0){ buf[_s] = ''; printf("client say : %s ",buf); } memset(buf,'',sizeof(buf)); printf("please Enter: "); fflush(stdout); ssize_t _s2 = read(0,buf,sizeof(buf) - 1); if(_s2 > 0){ write(fd,buf,strlen(buf)); } } } int main(int argc,char *argv[]) { if(argc != 3){ usage(argv[0]); exit(1); } //1.creat socket int sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(sock < 0){ perror("creat socket error "); return 1; } struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(atoi(argv[2])); local.sin_addr.s_addr = inet_addr(argv[1]); //2.bind if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0){ perror("bind error "); close(sock); return 2; } //3.listen if(listen(sock,10) < 0){ perror("listen error "); close(sock); return 3; } printf("bind and listen success!wait accept... "); //4.accept struct sockaddr_in peer; socklen_t len = sizeof(peer); while(1){ int fd = accept(sock,(struct sockaddr*)&peer ,&len); if(fd < 0){ perror("accept error "); close(sock); return 4; } printf("get connect,ip is : %s port is : %d ",inet_ntoa(peer.sin_addr),ntohs(peer.sin_port)); pthread_t id; pthread_create(&id,NULL,thread_run,(void*)fd); pthread_detach(id); } close(sock); return 0; }
    //client
    #include<stdio.h> #include<unistd.h> #include<sys/socket.h> #include<sys/types.h> #include<string.h> #include<errno.h> #include<netinet/in.h> #include<arpa/inet.h> static void usage(const char *proc) { printf("please use : %s [ip] [port] ",proc); } int main(int argc,char *argv[]) { if( argc != 3 ){ usage(argv[0]); exit(1); } int sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0){ perror("socket error"); return 1; } struct sockaddr_in remote; remote.sin_family = AF_INET; remote.sin_port = htons(atoi(argv[2])); remote.sin_addr.s_addr = inet_addr(argv[1]); int ret = connect(sock,(struct sockaddr*)&remote,sizeof(remote)); if(ret < 0){ printf("connect failed:%s ",strerror(errno)); return 2; } printf("connect success! "); char buf[1024]; while(1){ memset(buf,'',sizeof(buf)); printf("please enter:"); fflush(stdout); ssize_t _s = read(0,buf,sizeof(buf)-1); if(_s > 0){ buf[_s - 1] = ''; write(sock,buf,strlen(buf)); _s = read(sock,buf,sizeof(buf)-1); if(_s > 0){ if(strncasecmp(buf,"quit",4) == 0){ printf("qiut "); break; } buf[_s -1] = ''; printf("%s ",buf); } } } close(sock); return 0; }
  • 相关阅读:
    android webview cookie同步
    session和cookie
    对称加密与非对称加密
    理解java回调机制
    android studio命令
    android studio友盟gradle多渠道打包
    [c++] final override keyword
    [C++] Returning values by reference in C++
    [c++] polymorphism without virtual function
    [C++] NEW Advanced Usage
  • 原文地址:https://www.cnblogs.com/haibiandemoumoumou/p/14387599.html
Copyright © 2011-2022 走看看