加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux进程间通信:TCP

发布时间:2016-01-20 08:32:58 所属栏目:Linux 来源:网络整理
导读:Client.c #include sys/types.h> #include sys/socket.h> #include stdio.h> #include netinet/in.h> #include arpa/inet.h> #include unistd.h> #include stdlib.h> int main() { int sockfd; int len; struct sockaddr_in address; int result; int i,by

Client.c

#include <sys/types.h>    
#include <sys/socket.h>    
#include <stdio.h>    
#include <netinet/in.h>    
#include <arpa/inet.h>    
#include <unistd.h>    
#include <stdlib.h>    
int main()    
{    
    int sockfd;    
    int len;    
    struct sockaddr_in address;    
    int result;    
    int i,byte;    
    char char_recv,char_send;    
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)    
    {    
        perror("socket");    
        exit(EXIT_FAILURE);    
    }    
    address.sin_family = AF_INET;    
    address.sin_addr.s_addr = inet_addr("127.0.0.1");    
    address.sin_port = 8080;    
    len = sizeof(address);    
    if((result = connect(sockfd,(struct sockaddr *)&address,len)) == -1)    
    {    
        perror("connect");    
        exit(EXIT_FAILURE);    
    }    
         
    printf("please enter the context you want to send to server :");    
    scanf("%c",&char_send);    
         
    if(byte = send(sockfd,&char_send,1,0) == -1)    
    {    
        perror("send");    
        exit(EXIT_FAILURE);    
    }    
    if(byte = recv(sockfd,&char_recv,1,0) == -1)    
    {    
        perror("recv");    
        exit(EXIT_FAILURE);    
    }    
    printf("receive from server %cn",char_recv);    
             
    close(sockfd);    
    exit(0);    
}

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读