1. 程式人生 > >linux環境下基於udp socket簡單聊天通訊

linux環境下基於udp socket簡單聊天通訊

客戶端程式碼:client.c

/* 
 * File:   main.c
 * Author: guanyy
 *
 * Created on 20161202
 *
 * 主要實現:客戶端和服務端相互通訊
 */

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

int port=6789;
int main(int argc, char** argv) {
    int socket_descriptor; //套介面描述字
    int iter=0;
    char buf[256];
	int b_len = 256;
	
    struct sockaddr_in address;//處理網路通訊的地址
	int data_len;
	int a_len;

    bzero(&address,sizeof(address));
    address.sin_family=AF_INET;
    address.sin_addr.s_addr=inet_addr("192.168.25.134");//這裡不一樣
    address.sin_port=htons(port);
	a_len = sizeof(address);	

    //建立一個 UDP socket

    socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);//IPV4  SOCK_DGRAM 資料報套接字(UDP協議)
	if(socket_descriptor < 0)
	{
		perror("socket fail\n");
		exit(-1);
	}

	while(1)
    {
		bzero(buf,b_len);
      	scanf("%s",buf); 
        /*int PASCAL FAR sendto( SOCKET s, const char FAR* buf, int len, int flags,const struct sockaddr FAR* to, int tolen);  
         * s:一個標識套介面的描述字。 
         * buf:包含待發送資料的緩衝區。  
         * len:buf緩衝區中資料的長度。 
         * flags:呼叫方式標誌位。  
         * to:(可選)指標,指向目的套介面的地址。 
         * tolen:to所指地址的長度。  
        */
        sendto(socket_descriptor,buf,sizeof(buf),0,(struct sockaddr *)&address,a_len);
	
		bzero(buf,b_len);
		data_len = recvfrom(socket_descriptor,buf,b_len,0,(struct sockaddr *)&address,&a_len);
		printf("server info:%s\t%d\n",inet_ntoa(address.sin_addr),address.sin_port);
		printf("Response from server:%d\t%s\n",data_len,buf);
    }

    close(socket_descriptor);
    printf("Messages Sent,terminating\n");

    exit(0);

    return (EXIT_SUCCESS);
}


服務端程式碼:server.c

/* 
 * File:   main.c
 * Author: guanyy
 *
 * Created on 20161202
 */

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port=6789;

int main(int argc, char** argv) {

    int sin_len;
	int data_len;
    char message[256];
	int  m_len = 256;

    int socket_descriptor;
    struct sockaddr_in sin;
    printf("Waiting for data form sender \n");

    bzero(&sin,sizeof(sin));
    sin.sin_family=AF_INET;
    sin.sin_addr.s_addr=htonl(INADDR_ANY);
    sin.sin_port=htons(port);
    sin_len=sizeof(sin);

    socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);
    if((bind(socket_descriptor,(struct sockaddr *)&sin,sizeof(sin)) < 0))
	{
		perror("bind fail\n");
		exit(-1);
	}

    while(1)
    {
		bzero(message,m_len);
        data_len = recvfrom(socket_descriptor,message,m_len,0,(struct sockaddr *)&sin,&sin_len);
		printf("client info:%s\t%d\n",inet_ntoa(sin.sin_addr),sin.sin_port);
        printf("Response from server:%d\t%s\n",data_len,message);
        if(strncmp(message,"stop",4) == 0)//接受到的訊息為 “stop”
        {

            printf("Sender has told me to end the connection\n");
            break;
        }
		//sent message to client
		sendto(socket_descriptor,message,sizeof(message),0,(struct sockaddr *)&sin,sin_len);
		
    }

    close(socket_descriptor);
    exit(0);

    return (EXIT_SUCCESS);
}