c socket

server.c
服务端创建逻辑:

  1. 建立socket(socket())
  2. 绑定ip 与端口(bind())
  3. 监听客户端请求(accept())(阻塞)
  4. 给客户端返回数据(send())
    注意:通信是根据connectfd (int),可以当做是连接id,通过此id 可以找到客户端
    fd=file description 文件描述符
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<errno.h>
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>



#define LISTEN_PORT 8000
#define MAX_BYTES_LINE 4096


int main(int argc,char **argv){

int sockfd,connectfd;
int dataLen=0;
int i_listenPort=8000;

char buffer[MAX_BYTES_LINE];
char * c_ipAddr="127.0.0.1";
char * c_responseInfo="Hello,you are connected!\n";
struct sockaddr_in serverAddr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){

printf("create server socket error:%s(errno:%d)\n",strerror(errno),errno);
    exit(0);
}
memset(&serverAddr,0,sizeof(serverAddr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);//auto get local server ip addr
serverAddr.sin_port=htons(LISTEN_PORT);

if(bind(sockfd,(struct sockaddr*) & serverAddr,sizeof(serverAddr))==-1){
printf("bind socket error:%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
if(listen(sockfd,10)==-1){
    printf("listen socket error :%s(errno:%d)\n",strerror(errno),errno);
exit(0);
}
printf("waiting for client to connect\n");
while(1){
    if((connectfd=accept(sockfd,(struct sockaddr *)NULL,NULL))==-1){
       printf("accept socket error:%s(errorno:%d)",strerror(errno),errno);
     continue;
    }
    dataLen=recv(connectfd,buffer,MAX_BYTES_LINE,0);
    if(!fork()){ //in sub process ,fork() will return 0
        if(send(connectfd,c_responseInfo,strlen(c_responseInfo),0)==-1){
            perror("send response error");
            close(connectfd);
            exit(0);
        }
         buffer[dataLen]='\0';
         printf(" recv msg from client connectfd: %d,\ncontent:%s\n",connectfd,buffer);
        close(connectfd);// close connection
    }
   
}

close(sockfd);


}

client.c

客户端逻辑:

  1. 建立socket
  2. 连接server(connect())\
  3. 发送数据(send())
  4. 读取返回数据(recv())
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAX_BYTES_LINE 4096

int main(int argc, char **argv)
{

    int sockfd, n, recv_len;
    char recvLine[MAX_BYTES_LINE], 
         sendLine[MAX_BYTES_LINE];
    char buffer[MAX_BYTES_LINE];
    char *c_ipAddr = "127.0.0.1";
    int i_port = 8000;
    struct sockaddr_in serverAddr;
    if (argc == 1)
    {

        printf("this client will connect server message:ip=127.0.0.1,Prot=8000\n");
    }

    else if (argc == 2)
    {

        c_ipAddr = argv[1];
        printf("this client will connect server message:ip=%s,port=8000\n", c_ipAddr);
    }
    else if (argc == 3)
    {

        c_ipAddr = argv[1];
        i_port = atoi(argv[2]);
        printf("this client will connect server message:IP=%s,port=%d\n", c_ipAddr, i_port);
    }
    else
    {

        printf(" usage:./client <ipaddress> and port\n");
        exit(0);
    }

// 1. create client socket
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("create socket error:%s(errno:%d)\n", strerror(errno), errno);
        exit(0);
    }

    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(i_port);
    if (inet_pton(AF_INET, c_ipAddr, &serverAddr.sin_addr) <= 0)// 地址转换,10进制数转二进制
    {
        printf("inet_pton error for %s\n", argv[1]);
        exit(0);
    }
    // 2. connect to server 
    if(connect(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr))<0){
        printf("connect error :%s)errno:%d)\n",strerror(errno),errno);
        exit(0);
    }
    //3. send data to server
    printf("send msg to Server:\n");
    fgets(sendLine,MAX_BYTES_LINE,stdin);

    if(send(sockfd,sendLine,strlen(sendLine),0)==-1){
     printf("send msg error :%s(errno:%d)",strerror(errno),errno);
     exit(0);
    }
    //4, read data from server
    if((recv_len=recv(sockfd,buffer,MAX_BYTES_LINE,0))==-1){
        perror("recv error");
        exit(1);
    }
     buffer[recv_len]='\0';
     printf("received :%s",buffer);
     close(sockfd);
     exit(0);
    

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值