伯克利socketAPI

socket C语言的接口的介绍:

服务器开发虽然不是直接拿socketAPI进行开发但也需要了解

伯克利API:
API的历史:
在这里插入图片描述
头文件:
在这里插入图片描述
API函数:
在这里插入图片描述
在这里插入图片描述

最简单的服务器和对应的客户端C语言的实现

tcp socket基本流程图:
在这里插入图片描述
哪一边主动调用close(SocketFD);,哪边进入time_wait状态
close的时候也会把一些小数据先发送完再关闭,所以即使不关闭nagle算法也一样能收到数据

服务器部分:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
  char hello[] = "hello world";
  struct sockaddr_in sa;
  int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	

  if (-1 == SocketFD) {
    perror("cannot create socket");
    exit(EXIT_FAILURE);
  }

  memset(&sa, 0, sizeof sa);

  sa.sin_family = AF_INET;
  sa.sin_port = htons(2222);
  sa.sin_addr.s_addr = htonl(INADDR_ANY);

  if (-1 == bind(SocketFD, (struct sockaddr*)&sa, sizeof sa)) {
    perror("bind failed");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }

  if (-1 == listen(SocketFD, 10)) {
    perror("listen failed");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }

  for (;;) {
    int ConnectFD = accept(SocketFD, NULL, NULL);

    if (0 > ConnectFD) {
      perror("accept failed");
      close(SocketFD);
      exit(EXIT_FAILURE);
    }

    int writeSize = 0;
    size_t totalWrite = 0;
    while (totalWrite < sizeof(hello)) {
      writeSize =
          write(ConnectFD, hello + totalWrite, sizeof(hello) - totalWrite);
      if (-1 == writeSize) {
        perror("write failed");
        close(ConnectFD);
        close(SocketFD);
        exit(EXIT_FAILURE);
      }
      totalWrite += writeSize;
    }

    if (-1 == shutdown(ConnectFD, SHUT_RDWR)) {
      perror("shutdown failed");
      close(ConnectFD);
      close(SocketFD);
      exit(EXIT_FAILURE);
    }
    close(ConnectFD);
  }

  close(SocketFD);
  return EXIT_SUCCESS;
}

客户端部分:

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

int main(int argc, char** argv) {
  struct sockaddr_in sa;
  int res;
  int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (-1 == SocketFD) {
    perror("cannot create socket");
    exit(EXIT_FAILURE);
  }

  memset(&sa, 0, sizeof sa);

  sa.sin_family = AF_INET;
  sa.sin_port = htons(2222);
  res = inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr);

  if (-1 == connect(SocketFD, (struct sockaddr*)&sa, sizeof sa)) {
    perror("connect failed");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }

  char buffer[512];
	int totalRead = 0;
  for (;;) {
    int readSize = 0;
    readSize = read(SocketFD, buffer + totalRead, sizeof(buffer) - totalRead);
    if (readSize == 0) {
      // read all
      break;
    } else if (readSize == -1) {
      perror("read failed");
      close(SocketFD);
      exit(EXIT_FAILURE);
    }
		totalRead += readSize;
  }
	buffer[totalRead] = 0;
	printf("get from server: %s\n", buffer);
  /*  perform read write operations ... */

  (void)shutdown(SocketFD, SHUT_RDWR);
  close(SocketFD);
  return EXIT_SUCCESS;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值