TCP 및 UDP 편집 프로세스 및 코드

6320 단어 네트워크 통신

TCP 편집 프로세스:


서버 측:

  • socket - 클라이언트 연결을 감청하는 데 사용할 소켓을 만듭니다
  • bind - 감청 소켓을 서버 IP 주소 및 포트 번호와 연결합니다
  • listen - 감청 시작
  • accept - 클라이언트 연결을 가져오고 클라이언트 연결을 기록하는 플러그인을 되돌려줍니다
  • recv/send - 데이터를 수신하거나 발송합니다
  • close - 클라이언트 연결을 닫거나 감청 플러그인을 닫습니다(서버 닫기)

  • 클라이언트:

  • socket - 서버 연결 및 통신에 사용할 소켓을 만듭니다
  • connect - 연결을 시작합니다
  • recv/send - 데이터를 수신하거나 발송합니다
  • close - 연결을 닫습니다

  • TCP는 각 인터페이스 함수를 프로그래밍합니다.


    socket 만들기
        int   socket(int  domain,   int  type,   int  protocol);
    명령 소켓
        int   bind(int  sockfd, const struct sockaddr *addr,  socklen_t  addrLen);
    소켓 감청
        int   listen(int  sockfd,   int  backlog);
    링크 수신
        int  accept(int  sockfd, struct  sockaddr *addr,  socklen_t *addrLen);
    시작 링크
        int  connect(int  sockfd,  struct  sockaddr *serAddr,  socklen_t  addrLen);
    데이터 읽기 및 쓰기
    데이터 읽기
        int   recv(int  sockfd,   void  *buff,   size_t  buffSize,  int  flag);
    데이터를 쓰다
        int   write(int  sockfd,  const void *buff,  size_t  dataLen,  int  flag);
    연결 해제
        int   close(int   sockfd);

    UDP 편집 프로세스


    서버 측:

  • socket
  • recvfrom
  • sendto
  • close

  • 클라이언트:

  • socket
  • sendfrom
  • recvfrom
  • close

  • UDP 데이터 읽기 및 쓰기 인터페이스 함수


    데이터 읽기
        int   recvfrom(int sockfd,   void  *buff,  size_t  buffSize,  int  flag,  struct  sockaddr  *peerAddr,   socklen_t   *addrLen);
    데이터를 쓰다
        int   sendto( int sockfd,   void  *buff,  size_t  dataLen,  int  flag,  struct  sockaddr  *destAddr,   socklen_t   addrLen);

    TCPServer 코드는 다음과 같습니다.

    #include  
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include     //  IP 
    #include    //   
    
    int main()
    {
    	                   //   
    	int listenfd = socket(AF_INET,  SOCK_STREAM,  0);
    	assert(-1 != listenfd);
    
    	struct sockaddr_in  ser, cli;
    	memset(&ser, 0, sizeof(ser));
    	ser.sin_family = AF_INET;   //   
    	ser.sin_port = htons(6000);
    	inet_aton("127.0.0.1", (struct in_addr*)&ser.sin_addr);
    
    	int res = bind(listenfd, (struct sockaddr*)&ser, sizeof(ser));
    	assert(-1 != res);
    
    	listen(listenfd,  2);
    
    	while(1)
    	{
    		int  len = sizeof(cli);
    		//   c 
    		//   accept 
    		int c = accept(listenfd, (struct sockaddr*)&cli, &len); //  
    		assert(-1 != c);
    
    		while(1)
    		{
    			char buff[128] = {0};
    			int n = recv(c, buff,  2,  0); //      
    			if(n == 0)
    			{
    				printf("client  unlink
    "); close(c); break; } else if(n == -1) { printf("error
    "); close(c); break; } printf("n == %d: %s
    ", n, buff); send(c, "OK", 2, 0); } } close(listenfd); }

    TCPClient 코드는 다음과 같습니다.

    #include  
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include     //  IP 
    #include    //   
    
    
    int main()
    {
    	int sockfd = socket(AF_INET, SOCK_STREAM,  0);
    	assert(-1 != sockfd);
    
    	struct sockaddr_in ser;
    	memset(&ser, 0, sizeof(ser));
    	ser.sin_family = AF_INET;
    	ser.sin_port = htons(6000);
    	inet_aton("127.0.0.1", (struct in_addr*)&ser.sin_addr);
    
    	int res = connect(sockfd,  (struct sockaddr*)&ser, sizeof(ser));
    	assert(-1 != res);
    
    	while(1)
    	{
    		printf("please input: ");
    		char data[128] = {0};
    		fgets(data, 128, stdin);
    		if(strncmp(data, "bye", 3) == 0)
    		{
    			close(sockfd);
    			break;
    		}
    
    		send(sockfd, data, strlen(data) - 1, 0);
    
    		char buff[128] = {0};
    		int n = recv(sockfd, buff, 127, 0);
    		if(n <= 0)
    		{
    			printf("error
    "); close(sockfd); break; } printf("n == %d: %s
    ", n, buff); } }

    UDPServer 코드는 다음과 같습니다.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
    	int sockfd = socket(AF_INET,  SOCK_DGRAM, 0);
    	assert(-1 != sockfd);
    
    	struct sockaddr_in ser;
    	memset(&ser, 0, sizeof(ser));
    	ser.sin_family = AF_INET;
    	ser.sin_port = htons(6500);
    	ser.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	int res = bind(sockfd,  (struct sockaddr*)&ser, sizeof(ser));
    
    	while(1)
    	{
    		char buff[128] = {0};
    		struct sockaddr_in cli;
    		int len = sizeof(cli);
    
    		int n = recvfrom(sockfd, buff, 10, 0, (struct sockaddr*)&cli, &len);
    		if(n <= 0)
    		{
    			printf("error
    "); continue; } printf("ip: %s, port: %d
    ", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port)); printf("n == %d: %s
    ", n, buff); sendto(sockfd, "OK", 2, 0, (struct sockaddr*)&cli, sizeof(cli)); } }

    UDPClient 코드는 다음과 같습니다.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
    	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    	assert(-1 != sockfd);
    
    	struct sockaddr_in ser;
    	memset(&ser, 0, sizeof(ser));
    	ser.sin_family = AF_INET;
    	ser.sin_port = htons(6500);
    	ser.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	while(1)
    	{
    		printf("please input: ");
    		char data[128] = {0};
    		fgets(data, 128, stdin);
    
    		if(strncmp(data, "bye", 3) == 0)
    		{
    			close(sockfd);
    			break;
    		}
    
    		sendto(sockfd, data, strlen(data) - 1, 0, 
    			(struct sockaddr*)&ser, sizeof(ser));
    
    		char buff[128] = {0};
    		int n = recvfrom(sockfd, buff, 127, 0, NULL, NULL);
    		if(n <= 0)
    		{
    			printf("error
    "); break; } printf("n == %d: %s
    ", n, buff); } }

    좋은 웹페이지 즐겨찾기