星空网 > 软件开发 > 操作系统

6.1.2Linux下Socket编程

tcp简单实验

server.c

 

#include <sys/types.h>     /* See NOTES */#include <sys/socket.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdio.h>#include <signal.h>/*socket* bind* listen* accept* send/recv*/#define SERVER_PORT 8888#define BACKLOG   10int main(int argc, char **argv){  int iSocketServer;  int iSocketClient;  struct sockaddr_in tSocketServerAddr;  struct sockaddr_in tSocketClientAddr;  int iRet;  int iAddrLen;    int iRecvLen;  unsigned char ucRecvBuf[1000];   //接收缓冲区  int iClientNum = -1;  signal(SIGCHLD,SIG_IGN);  //等待为子进程收尸信号    /* AF_.NET为ipv4类型,SOCK_STREAM为Tcp连接类型,*/  iSocketServer = socket(AF_INET, SOCK_STREAM, 0);    if( -1 == iSocketServer)  {    printf("socket error!\n");        return -1;  }  tSocketServerAddr.sin_family   = AF_INET;  tSocketServerAddr.sin_port    = htons(SERVER_PORT);  /* host to net ,short */  tSocketServerAddr.sin_addr.s_addr = INADDR_ANY;       /* 本机上的所有IP */  memset(tSocketServerAddr.sin_zero, 0, 8);         /* 设置无用的空间为0 */    iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));  if( -1 == iRet)  {    printf("bind error!\n");         return -1;  }  iRet = listen(iSocketServer, BACKLOG);  if( -1 == iRet)  {    printf("listen error!\n");        return -1;  }  while(1)  {    iAddrLen = sizeof(struct sockaddr);    iSocketClient = accept(iSocketServer, (struct sockaddr *)&tSocketClientAddr, &iAddrLen);    if (-1 != iSocketClient)    {      iClientNum++;      printf("Get connect from client %d : %s\n", iClientNum, inet_ntoa(tSocketClientAddr.sin_addr));    if(!fork())    {      /* 子进程的源码 */      while(1)      {        /* 接收客户端信息并打印出来 */        iRecvLen = recv(iSocketClient, ucRecvBuf, 999, 0);        if(iRecvLen <= 0)        {          close(iSocketClient);          return -1;        }        else          {            ucRecvBuf[iRecvLen] = '\0';            printf("Get Msg From Client %d: %s\n", iClientNum, ucRecvBuf);          }        }      }    }  }    close(iSocketServer);  return 0;}

 

client.c

#include <sys/types.h>     /* See NOTES */#include <sys/socket.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdio.h>#include <signal.h>/*socket* connet* send/recv*/#define SERVER_PORT 8888int main(int argc, char **argv){  int iSocketClient;  struct sockaddr_in tSocketServerAddr;  int iRet;  unsigned char ucSendBuf[1000];  int iSendLen;    if(argc != 2)  {    printf("Usage:\n");    printf("%s<server_ip>\n", argv[0]);    return -1;  }    iSocketClient = socket(AF_INET, SOCK_STREAM, 0);    tSocketServerAddr.sin_family   = AF_INET;  tSocketServerAddr.sin_port    = htons(SERVER_PORT);  /* host to net ,short */  if(0 == inet_aton(argv[1], &tSocketServerAddr.sin_addr)) /* 本机上的所有IP */  {    printf("invalid server_ip\n");    return -1;  }  memset(tSocketServerAddr.sin_zero, 0, 8);         /* 设置无用的空间为0 */      iRet = connect(iSocketClient, (struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));  if(iRet == -1)  {    printf("connect error!\n");        return -1;  }  while(1)  {    if(fgets(ucSendBuf, 999, stdin))    {      iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0);      if(iSendLen <= 0)      {        close(iSocketClient);        return -1;      }    }  }  return 0;}




原标题:6.1.2Linux下Socket编程

关键词:linux

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

给国外账户汇款:https://www.goluckyvip.com/tag/72922.html
国外期货交易开户:https://www.goluckyvip.com/tag/72923.html
境外账户汇款国外:https://www.goluckyvip.com/tag/72925.html
bvi境外开户:https://www.goluckyvip.com/tag/72927.html
节日季期间:https://www.goluckyvip.com/tag/7293.html
在国外期货开户:https://www.goluckyvip.com/tag/72930.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
零售晚报:丽人丽妆2023年扭亏为盈 玉容初、美壹堂等自有品牌增速超40% :https://www.kjdsnews.com/a/1836649.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流