Ads 468x60px

##EasyReadMore##

28 4月, 2016

[C/C++] 實現 Socket Server , Slient

實作:

Server 端:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define BUF_SIZE 1024

int main()
{
    int sockfd;
    int port = 8591;
    int rc;
    struct sockaddr_in dest;
    char buffer[BUF_SIZE] = "Hi client! ";
    char buffer1[BUF_SIZE];
    /* create sockett */
    sockfd = socket(PF_INET, SOCK_STREAM, 0);

    /* initialize structure dest */
    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(port);
    /* this line is different from client */
    dest.sin_addr.s_addr = INADDR_ANY;

    /* Assign a port number to socket */
    rc = bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));
    if(rc == -1){
    printf("billy[%s:%d,%s] Error! bind rc = %d\n", __FILENAME__, __LINE__, __FUNCTION__,rc);
    return -1;
    }
    printf("billy[%s:%d,%s] bind port = %d\n", __FILENAME__, __LINE__, __FUNCTION__,port);

    /* make it listen to socket with max 20 connections */
    if((rc = listen(sockfd, 20)) == -1 ){
    printf("billy[%s:%d,%s] Error! listen rc = %d\n", __FILENAME__, __LINE__, __FUNCTION__,rc);
    return -1;
    }
    printf("billy[%s:%d,%s] Success! listen rc = %d\n", __FILENAME__, __LINE__, __FUNCTION__,rc);
 

    /* infinity loop -- accepting connection from client forever */
    while(1)
    {
        int clientfd;
        struct sockaddr_in client_addr;
        int addrlen = sizeof(client_addr);

        /* Wait and Accept connection */
        if((clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen)) == -1){
printf("billy[%s:%d,%s] Error! accept rc = %d\n", __FILENAME__, __LINE__, __FUNCTION__,clientfd);
return -1;
        }
printf("billy[%s:%d,%s] Success! accept rc = %d\n", __FILENAME__, __LINE__, __FUNCTION__,clientfd);

        /* Send message */
        send(clientfd, buffer, sizeof(buffer), 0);
        int res=recv(clientfd, buffer1, sizeof(buffer1), 0);
        printf("billy[%s:%d,%s] receive from client: %s, %d bytes\n", __FILENAME__, __LINE__, __FUNCTION__, buffer1,res);
        //write(fd, buffer1, sizeof(buffer1));
     
        /* close(client) */
        close(clientfd);
    }

    /* close(server) , but never get here because of the loop */
    close(sockfd);
    return 0;
}


Client 端:

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define BUF_SIZE 1024

int main()
{
    int sockfd;
    struct sockaddr_in dest;
    char buffer[BUF_SIZE];
    char resp[BUF_SIZE]="Hi service";
    int port = 8591;
    char s_ip[64] = "127.0.0.1";

    /* create socket */
    sockfd = socket(PF_INET, SOCK_STREAM, 0);

    /* initialize value in dest */
    bzero(&dest, sizeof(dest));
    dest.sin_family = PF_INET; /* IP protocol family.  */
    dest.sin_port = htons(port);
    dest.sin_addr.s_addr = inet_addr(s_ip);
    //inet_aton("127.0.0.1", &dest.sin_addr);

    /* Connecting to server */
    connect(sockfd, (struct sockaddr*)&dest, sizeof(dest));

    /* Receive message from the server and print to screen */
    if((rc = send(sockfd,buffer,strlen(buffer),0)) == -1 ){
    printf("billy[%s:%d,%s] Socket send error\n", __FILENAME__, __LINE__, __FUNCTION__);
    close(sockfd);
    return rc;
    }

    bzero(buffer, BUF_SIZE);
    recv(sockfd, buffer, sizeof(buffer), 0);
    printf("billy[%s:%d,%s] receive from server: %s\n", __FILENAME__, __LINE__, __FUNCTION__,buffer);

 
    /* Close connection */
    close(sockfd);

    return 0;
}

注意:

電腦的防火牆要關 不然會接收不到

結果:

Server 端:

billy[test2.c:36,main] bind port = 8591
billy[test2.c:43,main] Success! listen rc = 0
billy[test2.c:58,main] Success! accept rc = 4
billy[test2.c:63,main] receive from client: Hi service, 1024 bytes

Client 端:

billy[RClient.c:36,main] receive from server: Hi client!

socket 範例程式--client / server communication @ 心的距離 :: 痞客邦 PIXNET :: - http://goo.gl/G8MSW

0 意見:

張貼留言

 
Blogger Templates