Linux_socket编程入门(C++)

2018-04-23  本文已影响0人  AnswerHua

Linux_socket编程入门(C++)

参考资料

参考:这里还有这里

实现思路

Socket编程主要分为两部分,Server端以及Client端,以下依次从这两方面开始讲解。

Server端

需要完成的任务

所用到的一些函数以及结构

Client端

需要完成的任务

所用到的一些函数以及结构

源代码

server.cpp

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

using namespace std;

int main()
{
    int client, server;
    int portNum = 1500;
    bool isExit = false;
    int bufsize = 1024;
    char buffer[bufsize];
    
    struct sockaddr_in server_addr; 
    socklen_t size;
    
    //init socket.
    
    client = socket(AF_INET, SOCK_STREAM, 0); //AF_INET指明使用TCP/IP协议簇
                                              //SOCK_STREAM指明使用流式嵌套字
                                              //使用default 协议类型 
    
    if (client < 0) //返回值为-1,连接失败 
    {
        cout<<"=> Error establishing connection."<<endl;
        exit(1);    
    }
    
    cout<<"\n=> Socket server has been created."<<endl;
    
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY能够收到任意一块网卡的连接 
    server_addr.sin_port =  htons(portNum); 

    //binding socket

    if(bind(client, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)
    {
        cout<<"=> Error binding socket..."<<endl;
        exit(1);
    }
    
    size = sizeof(server_addr);
    cout<<"=> Looking for client"<<endl;
    
    //listening socket
    
    listen(client,1);
    
    //accept client
    
    server = accept(client, (struct sockaddr*)&server_addr,&size);
    
    if(server < 0)
    {
        cout<<"=> Error on accepting..."<<endl;
        exit(1);    
    }    
    
    while(server > 0)
    {
        strcpy(buffer,"Server connected...\n");
        send(server, buffer, bufsize, 0);
        cout<<"=> Connected with client..."<<endl;
        cout<<"\n=> Enter # to end the connection\n"<<endl;
        
        cout<<"Client: ";
        do{
            recv(server, buffer, bufsize, 0);
            cout << buffer << " ";
            if(*buffer == '#')
            {
                *buffer = '*';
                isExit = true;
            }
        }while(*buffer != '*');
        
        do{
            cout<<"\nServer: ";
            do{
                cin >> buffer;
                send(server, buffer, bufsize, 0);
                if(*buffer == '#')
                {
                    send(server, buffer, bufsize, 0);
                    *buffer = '*';
                    isExit = true;
                }
            }while (*buffer != '*');
            
            cout<<"Client: ";
            do{
                recv(server, buffer, bufsize, 0);
                cout << buffer <<" ";
                if(*buffer == '#')
                {
                    *buffer == '*';
                    isExit = true;
                }
            }while(*buffer != '*');
        }while(!isExit);
        
        cout<<"\n\n=> Connection terminated..."<< endl;
        cout<<"=> Goodbye..." << endl;
        isExit = false;
        exit(1);
    }
    close(client);
    return 0;   
}

client.cpp

/*
    Note the server doesn't need to know the clients port number
    but the client needs to the servers port number
*/

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

using namespace std;

int main()
{
    int client, server;
    int portNum = 1500; // note the server and clients Ip are the same
    bool isExit = false;
    int bufsize = 1024;
    char buffer[bufsize];
//  char *ip = "127.0.0.1";
    
    struct sockaddr_in server_addr;
    
    //init socket
    
    client = socket(AF_INET, SOCK_STREAM, 0);
    
    if (client < 0)
    {
        cout<<"\n=> Error creating socket."<<endl;
        exit(1);    
    }

    cout<<"\n=> Socket client has been created" <<endl;
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_port =  htons(portNum);
    
    //connecting socket server
    
    if(connect(client, (struct sockaddr*)&server_addr, sizeof(server_addr)) == 0)
    {
        cout << "=> Connecting to server..."<< endl;
    }
    
    recv(client, buffer, bufsize, 0);
    cout<< "=> Connection confirmed" << endl;
    
    cout<< "\n=> Enter # to end the connection\n" << endl;
    
    do{
        cout<<"Client: ";
        do{
            cin >> buffer;
            send(client, buffer, bufsize, 0);
            if(*buffer == '#')
            {
                send(client, buffer, bufsize, 0);
                *buffer = '*';
                isExit = true;
            }
        } while(*buffer != 42);
        
        cout<<"Server: ";
        
        do{
            recv(client, buffer, bufsize, 0);
            cout << buffer << " ";
            if(*buffer == '#')
            {
                *buffer = '*';
                isExit = true;
            }
        }while(*buffer != 42);
        
        cout<<endl;
    }while(!isExit);
    
    cout<<"\nConnection terminated..."<< endl;
    cout<<"Goodbye..." << endl;
    
    close(client);

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读