Linux 플랫폼 에서 TCP 프로 토 콜 기반 클 라 이언 트 와 서버 통신

7526 단어 C 언어
서버 코드
/*Server.c*/
#include               //        perror  
#include              //          bzero   strcmp
#include              //    
#include           //         
#include          // socket   bind         
#include  
#include          // struct sockaddr_in       
#include           // ip     inet_ntoa  
#include              //       ,close  
#include               //               
  
// #define SERVER_PORT 4800
#define BUFFER_LEN 1024
  
int main(int argc,char* argv[])
{  
    //  socket            
    int sockfd;  
    //       sockaddr_in     ,             
    struct sockaddr_in server_addr;  
    struct sockaddr_in client_addr;  
    int addr_len = sizeof(client_addr);
    int connfd;
    char buffer[BUFFER_LEN];
    int n;

    //        3  ./Server ip ip_port
    if (argc != 3)
    {
        fputs("Usage: ./Server serverIP serverPORT
", stderr); exit(1); } /********************************************************************************** // :int socket(int domain, int type, int protocol) // : ,AF_INET(IPv4) AF_INET6(IPv6); ,SOCK_STREAM(TCP) SOCK_DGRAM(UDP); , 0 // : -1, ***********************************************************************************/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("sockfd"); exit(1); } // n ‘\0’, , 。 bzero(&server_addr, sizeof(server_addr)); // server_addr.sin_family = AF_INET; // ,unsigned short // atoi server_addr.sin_port = htons(atoi(argv[2])); /* ,unsigned long ip ip, INADDR_ANY(0.0.0.0) , */ if (inet_aton(argv[1], &server_addr.sin_addr) == 0) { perror("inet_aton"); exit(1); } /********************************************************************************** // bind,accept , (struct sockaddr*) // bind : , // bind : IP sock ***********************************************************************************/ if(bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("connect"); exit(1); } // TCP 5, // 0, -1 if(listen(sockfd, 5) < 0) { perror("listen"); exit(1); } printf("Listening on port: %d
", atoi(argv[2])); // char *inet_ntoa(struct in_addr addr) IP // , NULL* printf("IP is %s
", inet_ntoa(client_addr.sin_addr)); printf("Port is %d
", htons(client_addr.sin_port)); while(1) { /********************************************************************************** :int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) : ; ; , , , , -1 ***********************************************************************************/ connfd = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&addr_len); if(connfd < 0) { perror("accept"); exit(1); } // , memset(buffer, 0, BUFFER_LEN); n = recv(connfd, buffer, BUFFER_LEN, 0); if(n < 0) { perror("fail to receive"); exit(1); } else { // buffer[n] = '\0'; if(strcmp(buffer, "exit") == 0) break; printf("receive %d words is: %s
", n, buffer); // printf("Reply:>
"); gets(buffer); n = send(connfd, buffer, n, 0); if(n == -1) { perror("Fail to reply
"); exit(1); } } } }

클 라 이언 트 코드
/*Client.c*/  
#include               //        perror  
#include              //          bzero   strcmp
#include              //    
#include           //         
#include          // socket   bind         
#include  
#include          // struct sockaddr_in       
#include           // ip     inet_ntoa  
#include              //       ,close  
#include               //               

#define BUFFER_LEN 1024
  
int main(int argc,char* argv[])
{ 
    //                ,        
    int sockfd;
    int con;
    struct sockaddr_in serverAddr;
    socklen_t addr_len = sizeof(serverAddr);
    char buffer[BUFFER_LEN];    
    int n;

    //        3  ./Client ip ip_port
    if (argc != 3)
    {
        fputs("Usage: ./Server serverIP serverPORT
", stderr); exit(1); } if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("sockfd"); return -1; } printf("sockfd:%d
",sockfd); // n ‘\0’, , 。 bzero(&serverAddr, sizeof(serverAddr)); // serverAddr.sin_family = AF_INET; // serverAddr.sin_port = htons(atoi(argv[2])); // ip if (inet_aton(argv[1], &serverAddr.sin_addr) == 0) { perror("inet_aton"); exit(1); } /********************************************************************************** int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); sockfd TCP , ; addr 0, -1 ***********************************************************************************/ con = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); if( con < 0 ) { perror("connect"); return -1; } printf("connect with destination host...
"); // , memset(buffer, 0, BUFFER_LEN); printf("Input your world:>"); gets(buffer); printf("
"); n = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); if (n == -1) { perror("Fail to send"); exit(1); } else { if(strcmp(buffer, "exit") == 0) { exit(0); } else { memset(buffer, 0, BUFFER_LEN); n = recvfrom(sockfd, buffer, BUFFER_LEN, 0, (struct sockaddr *)&serverAddr, &addr_len); if (n == -1) { perror("Fail to receive"); exit(1); } else { buffer[n] = '\0'; printf("Receive data: %s
", buffer); } } } if( close(sockfd) == -1) { perror("fail to close"); exit(1); } puts("TCP Client is closed!
"); return 0; }

컴 파일: gcc - Woll Server. c - o Server gcc - Woll Client. c - o Client
실행:... / server 127.0.1 4800. / Client 127.0.0.1 4800

좋은 웹페이지 즐겨찾기