Chinaunix首页 | 论坛 | 博客
  • 博客访问: 646872
  • 博文数量: 121
  • 博客积分: 4034
  • 博客等级: 上校
  • 技术积分: 1439
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 12:42
文章分类

全部博文(121)

文章存档

2017年(8)

2016年(10)

2013年(2)

2012年(3)

2011年(18)

2010年(80)

分类: LINUX

2010-10-21 23:05:57

通过socket实现c和python的通信
这里的任何一个client和server都可有通信:
c语言 tcp_server.c:
/* 服务器端程序 */
#include
#include
#include
#include
#include
#include
#include
#include
 
#include
#include
#include
#include
 
#define MYPORT 5000
 
char buf[80];
int main(void)
{
    int sock, new_sd, adrlen, cnt;
    int re_use_addr=1;
    int ret;
    int status;
    pid_t pid;
    pid_t pc;
 
    struct sockaddr_in myname;
    struct timeval nNetTimeout;
    nNetTimeout.tv_sec = 5;
    nNetTimeout.tv_usec = 0;
 
    sock = socket(AF_INET, SOCK_STREAM, 0);        /* 套接字的建立 */
    if (sock < 0)
    {
        printf("server socket failure %d\n", errno);
        perror("server: ");
        exit(1);  
    }
    myname.sin_family = AF_INET;
    myname.sin_port = htons(MYPORT);
    myname.sin_addr.s_addr = htonl(INADDR_ANY);
    bzero(&(myname.sin_zero), 8);
 
    ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&re_use_addr, sizeof(int));
    if(ret<0)
    {
        perror("setsockopt:");
        return ret;
    }
 
    adrlen = sizeof(struct sockaddr_in);
    if (bind(sock, (struct sockaddr *)&myname, adrlen) < 0) 
    {
        printf("server bind failure %d\n", errno);
        perror("server: ");
        exit(1);
    }
    if (listen(sock, 5) < 0) 
    {        /* listen函数调用 */
        printf("server listen failure %d\n", errno);
        perror("server: ");
        exit(1);  
    }  
 
    /* 忽略子进程的终止  */
//    signal (SIGCHLD, SIG_IGN);
    /*  将服务器置于死循环状态,等待客户端的连接请求的到来。
        实际上,应该用更好的办法来结束这个死循环,在这里这
        个循环是被连接开始信号或者是超级用户终止的。     */
 
#if 0
    //发送时限
    ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&nNetTimeout, sizeof(nNetTimeout));
    if(ret < 0)
    {
        perror("server");
    }
    //接收时限
    ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&nNetTimeout, sizeof(nNetTimeout));
    if(ret < 0)
    {
        perror("server");
    }
#endif
     
    while (1)
    {
        if ((new_sd = accept(sock, (struct sockaddr *)&myname, &adrlen)) < 0)
        {
            printf("server accept failure %d\n", errno);
            perror("server: ");
            exit(1);
        }
 
        pc = fork();
        if(pc<0)
        {
            perror("[server]");
        }
        if (pc == 0) 
        {  /* 建立子进程 */
            close (sock);  /* 关闭套接字,因为子进程不再需要它  */
 
            memset(buf,0,sizeof(buf));
            cnt = recv(new_sd, buf, sizeof(buf),0);
            if(cnt<0)
            {
                perror("[server]");
            }
            else if(cnt == 0)
            {
                printf(" connection closed by client\n");
            }
            else
            {
                printf("Server got message:[%d]%s\n",cnt,buf);
            }
 
            memset(buf,0,sizeof(buf));
            strcpy(buf, "[dancy]send message to client:");
            cnt = write(new_sd, buf, sizeof(buf));
 
            close (new_sd); /* close prior to exiting  */
            exit(1);
        }
    }
 
    close (new_sd); /* close prior to exiting  */
    close (sock);  /* 关闭套接字,因为子进程不再需要它  */
}


c语言 tcp_client.c
/* 客户端程序 */
#include
#include
#include
#include
#include
#include
#include
#include
 
#include
#include
#include
#include
 
#define SERVER_PORT 5000
 
char buf[80];
struct sockaddr_in myname;
int main(void)
{
    int sock, adrlen, cnt;
    int ret;
    struct hostent *h;
 
    if (((h=gethostbyname("192.168.1.100")) == NULL))
    {
        herror("gethostbyname");
        exit(1);
    }
 
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) 
    {
        printf("client socket failure %d\n", errno);
        perror("client: ");
        exit(1);  
    }
 
    myname.sin_family = AF_INET;
    myname.sin_port = htons(SERVER_PORT);
    myname.sin_addr = *((struct in_addr *)h->h_addr);
    bzero(&(myname.sin_zero), 8);
 
    ret = connect(sock, (struct sockaddr *) &myname, sizeof(struct sockaddr));
    if(ret<0) 
    {
        perror("cannot connect ");
        exit(1);
    }
 
    memset(buf,0,sizeof(buf));
    strcpy(buf, "[dancy]send message to server");
    cnt = send(sock, buf, strlen(buf),0);
 
    memset(buf,0,sizeof(buf));
    cnt = read(sock, buf, sizeof(buf));
    printf("Client got message:[%d]%s\n", cnt,buf);
    exit(0);
}


C语言 Makefile
  1 BINS=tcp_client tcp_server
  2
  3 #CC=arm-linux-gcc
  4 CC=gcc
  5
  6 all: $(BINS)
  7 #$(MBINS)
  8
  9 $(BINS): % : %.c
 10     $(CC)  -o $@ $<
 11
 12 #$(MBINS): % : %.c
 13 #   $(CC)  -o $@ $<
 14
 15 clean:
 16     -rm -f *.o $(BINS)




python tcp_server.py

  1 #!/usr/bin/env python
  2 import socket
  3
  4 if __name__ == '__main__':
  5     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6     sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  7     sock.bind(('', 5000))
  8     sock.listen(5)
  9     while True:
 10         connection,address = sock.accept()
 11         try:
 12             connection.settimeout(5)
 13             buf = connection.recv(1024)
 14             print buf
 15
 16             connection.send('send message to client')
 17
 18         except socket.timeout:
 19             print 'time out'
 20
 21         connection.close()
 22     sock.close()


python tcp_client.py
  1 #!/usr/bin/env python
  2
  3 import socket
  4 import time
  5
  6 if __name__ == '__main__':
  7    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8    sock.connect(('192.168.1.100', 5000))
  9    sock.send('send message to server')
 10    print sock.recv(1024)
 11    sock.close()



阅读(5794) | 评论(0) | 转发(0) |
0

上一篇:子网掩码

下一篇:接受处理killall信号

给主人留下些什么吧!~~