本地socket与网络socket实现

Linux进程间通信方式–本地socket

服务端:

//s_unix.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> 
#define UNIX_DOMAIN "/tmp/UNIX.domain"
int main(void)
{
    socklen_t clt_addr_len;
    int listen_fd;
    int com_fd;
    int ret;
    int i;
    static char recv_buf[1024]; 
    int len;
    struct sockaddr_un clt_addr;
    struct sockaddr_un srv_addr;
    listen_fd=socket(PF_UNIX,SOCK_STREAM,0);
    if(listen_fd<0)
    {
        perror("cannot create communication socket");
        return 1;
    }  

    //set server addr_param
    srv_addr.sun_family=AF_UNIX;
    strncpy(srv_addr.sun_path,UNIX_DOMAIN,sizeof(srv_addr.sun_path)-1);
    unlink(UNIX_DOMAIN);
    //bind sockfd & addr
    ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
    if(ret==-1)
    {
        perror("cannot bind server socket");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    //listen sockfd 
    ret=listen(listen_fd,1);
    if(ret==-1)
    {
        perror("cannot listen the client connect request");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    //have connect request use accept
    len=sizeof(clt_addr);
    com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&len);
    if(com_fd<0)
    {
        perror("cannot accept client connect request");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    //read and printf sent client info
    printf("/n=====info=====/n");
    for(i=0;i<4;i++)
    {
        memset(recv_buf,0,1024);
        int num=read(com_fd,recv_buf,sizeof(recv_buf));
        printf("Message from client (%d)) :%s/n",num,recv_buf);  
    }
    close(com_fd);
    close(listen_fd);
    unlink(UNIX_DOMAIN);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

客户端

//c_unix.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define UNIX_DOMAIN "/tmp/UNIX.domain"
int main(void)
{
    int connect_fd;
    int ret;
    char snd_buf[1024];
    int i;
    static struct sockaddr_un srv_addr;
//creat unix socket
    connect_fd=socket(PF_UNIX,SOCK_STREAM,0);
    if(connect_fd<0)
    {
        perror("cannot create communication socket");
        return 1;
    }   
    srv_addr.sun_family=AF_UNIX;
    strcpy(srv_addr.sun_path,UNIX_DOMAIN);
//connect server
    ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
    if(ret==-1)
    {
        perror("cannot connect to the server");
        close(connect_fd);
        return 1;
    }
    memset(snd_buf,0,1024);
    strcpy(snd_buf,"message from client");
//send info server
    for(i=0;i<4;i++)
        write(connect_fd,snd_buf,sizeof(snd_buf));
    close(connect_fd);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

Socket 通信之网络通信

服务端

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>


#define SIZE 100

int main(int argc, char **argv)
{
      int sock_fd;
      struct sockaddr_in sin;
      //创建套接字
      if( (sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0){
        perror("socket");
        exit(1);
      }

      //填充地址
      bzero(&sin,sizeof(sin));
      sin.sin_family = AF_INET;
      if(argc != 3){
        sin.sin_port = htons(8888);
        sin.sin_addr.s_addr = inet_addr("192.168.7.5");
      }else{
        sin.sin_port = htons(atoi(argv[1]));
        sin.sin_addr.s_addr = inet_addr(argv[2]);
      }

      if(connect(sock_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
        perror("connect");
        exit(1);
      }


      //和客户端进行通信
      char buf[SIZE];
      while(1){
        fgets(buf,SIZE,stdin);
        write(sock_fd,buf,strlen(buf));
        if(strncmp(buf,"quit",4) == 0)
          break;
      }

      close(sock_fd);
      return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

客户端

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>


#define SIZE 100

int main(int argc, char **argv)
{
      int sock_fd,conn_fd;
      struct sockaddr_in sin,cin;
      //创建套接字
      if( (sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0){
        perror("socket");
        exit(1);
      }

      //填充地址
      bzero(&sin,sizeof(sin));
      sin.sin_family = AF_INET;
      if(argc != 3){
        sin.sin_port = htons(8888);
        sin.sin_addr.s_addr = inet_addr("192.168.7.5");
      }else{
        sin.sin_port = htons(atoi(argv[1]));
        sin.sin_addr.s_addr = inet_addr(argv[2]);
      }

      //绑定地址
      if(bind(sock_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
        perror("bind");
        exit(1);
      }
      //监听套接字
      if(listen(sock_fd,8) < 0){
        perror("listen");
        exit(1);
      }
      //等待接收链接,使进程阻塞,直到有新的客户端请求链接为止
      bzero(&cin,sizeof(cin));
      socklen_t len = sizeof(cin);
      printf("listen....\n");
      if( (conn_fd = accept(sock_fd,(struct sockaddr*)&cin,&len)) < 0){
        perror("accept");
        exit(1);
      }
      printf("客户端%s链接成功!\n",inet_ntoa(cin.sin_addr));
      //和客户端进行通信
      char buf[SIZE];
      int ret;
      while(1){
        bzero(buf,SIZE);
        if( (ret = read(conn_fd,buf,SIZE)) < 0){
          perror("read");
          exit(1);
        }else if( ret == 0){
          printf("客户端已断开\n");
          break;
        }else{
          if(strncmp(buf,"quit",4) == 0)
            break;
          printf("%s",buf);
        }
      }

      close(sock_fd);
      close(conn_fd);
      return 0;
}
阅读更多
个人分类: 工作常用
上一篇进程间通信之:socket
下一篇linux中的find命令——查找文件名
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭