socket實例 client

#include<netinet/in.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#define BUFFERSIZE 1024
void recieveData(int client_socket)                       //the function of handlding the data recieved
{
    char r_buffer[BUFFERSIZE];                        // the buffer for recieving
    bzero(r_buffer,BUFFERSIZE);                    
    int length=0;        
    int i;
    while(length=recv(client_socket,r_buffer,BUFFERSIZE,0))  //while the size of the string recieved is larger than 0
    {
        if(length<=0)                                   
        {
            printf("Recieve Data From Server Failed!\n");
            break;   
        }
        for(i=0;i&lt;length;i++)                           //print every single value of type char from the string recieved
        {
            printf("%c",r_buffer[i]);       
        }
    }
}
void option_T_N_L(char option,int client_socket)        //the function of handling the time,name,disconn,conn option
{
    char s_buffer[BUFFERSIZE];                       
    int i;
    bzero(s_buffer,BUFFERSIZE);
    s_buffer[0]=option;                             //set the specific option
    send(client_socket,s_buffer,BUFFERSIZE,0);
}
void option_S(int client_socket,int id)                 //the function of seding message to the server
{
    char msg[100];
    char s_buffer[BUFFERSIZE];
    bzero(s_buffer,BUFFERSIZE);       
    s_buffer[0]='S';                                 
    s_buffer[1]=id;                                 //the id in the listed clients from the sever
    s_buffer[2]='\0';
    printf("plz input the msg you want to send:\n");
    scanf("%s",msg);
    strcat(s_buffer,msg);
    send(client_socket,s_buffer,BUFFERSIZE,0);
}

int main()
{
    int connect_index=-1;                         //for judging if the connection is successful
    struct sockaddr_in client_addr;               //initialize the sockaddr_in for client
    char input[100];                              //string for command
    char address[16];                             //address for connecting
    char port[4];                                 //port for connecting
    char id[8];                                   //id for sending message
    pthread_t t1;
    bzero(&client_addr,sizeof(client_addr));       
    bzero(id,sizeof(id));
    client_addr.sin_family=AF_INET;                  
    client_addr.sin_addr.s_addr=htons(INADDR_ANY);  //the ip of localhost
    client_addr.sin_port=htons(0);                  //any idle port will set for client
    int client_socket=socket(AF_INET,SOCK_STREAM,0);
    if(client_socket&lt;0)
    {
        printf("Create Socket Failed!\n");
    }
    recieveData(client_socket);
    if(bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))//bind the socket get before and the client_addr
    {
        printf("Client Bind Port Failed!\n");
        exit(1);
    }
    printf("plz,input what you want to do :\n");
    printf("1.conn &lt;ip> <port>\n");
    printf("2.disconn\n");
    printf("3.time\n");
    printf("4.name\n");
    printf("5.list\n");
    printf("6.send <id>\n");
    printf("7,quit\n");
    scanf("%[^\n]",input);
    while(input)
    {
        if(!bcmp(input,"conn",4))                 //if the command is conn <ip> <port>, do...
        {
            int i=4;
            bzero(address,sizeof(address));
            while(input[++i]!='\0'&&input[i]!=' ')//get the address for connecting
            {
                address[i-5]=input[i];
            }
            if(connect_index!=-1)
            {
                printf("you have connected to:%s\n",address);
                memset(input,0,sizeof(input));
                continue;option_S('T',client_socket);
            }
            int temp=i+1;
            while(input[++i]!='\0')             //get the port for connecting
            {
                port[i-temp]=input[i];
            }       
            struct sockaddr_in server_addr;        //initialize the server_addr
            bzero(&server_addr,sizeof(server_addr));
            server_addr.sin_family=AF_INET;
            if(inet_aton(address,&server_addr.sin_addr)==0)
            {
                printf("Server IP Address Error!\n");   
                exit(1);
            }
            server_addr.sin_port=htons(atoi(port));
            socklen_t server_addr_length=sizeof(server_addr);
            connect_index=connect(client_socket,(struct sockaddr*)&server_addr,server_addr_length);    //connect the server   
            if(connect_index<0)           
            {
                printf("Can Not Connect To %s!\n",input);
                exit(1);
            }
            else
            {
                if(fork()==0)
                {
                    recieveData(client_socket);
                }
            }
            memset(port,0,sizeof(port));
            memset(input,0,sizeof(input));
        }
        else if(!bcmp(input,"time",4))     // if the command is time, do...
        {
            if(connect_index>=0)
            {
                    option_T_N_L('T',client_socket);
            }
            else
                printf("connect the server first,plz.\n");
            memset(port,0,sizeof(port));
        }
        else if(!bcmp(input,"name",4))   //if the command is name, do...
        {
            if(connect_index&gt;=0)
            {
                    option_T_N_L('N',client_socket);
            }
            else
                printf("connect the server first,plz.\n");
            memset(port,0,sizeof(port));
        }
        else if(!bcmp(input,"list",4))    //if the command is list, do...
        {
            if(connect_index&gt;=0)
            {
                    option_T_N_L('L',client_socket);
            }
            else
                printf("connect the server first,plz.\n");
            memset(port,0,sizeof(port));
        }
        else if(!bcmp(input,"send",4))  //if the command is send, do...
        {
            if(connect_index&gt;=0)
            {
                    int j=4;
                    while(input[++j]!='\0'&&input[j]!=' ')
                    {
                        id[j-5]=input[j];
                    }
                    option_S(client_socket,atoi(id));
                    memset(id,0,sizeof(id));
            }
            else
                printf("connect the server first,plz.\n");
            memset(port,0,sizeof(port));
            memset(id,0,sizeof(id));
        }
        else if(!bcmp(input,"disconn",7)) //if the command is disconn, do...
        {
            if(connect_index&gt;=0)
            {
                close(client_socket);
                printf("close the connection!\n");
                connect_index=-1;
                option_T_N_L('D',client_socket);
            }
            else
                printf("connect the server first,plz.\n");
        }
        else if(!bcmp(input,"quit",4))
        {
            close(client_socket);
            exit(1);
        }
        gets(input);   
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章