伯克利socketAPI

socket C语言的接口的介绍:

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

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

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

服务器部分:

#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;
}
发布了160 篇原创文章 · 获赞 43 · 访问量 7714

猜你喜欢

转载自blog.csdn.net/qq_39885372/article/details/104117019