开发者

socket programming( server and client on the same computer) something wrong with connection

I'm new to socket programming and I have this client that tries to connect to a server on the same computer. But the server hangs there after bind or accept—cause bind seems to be right but no output. I know that the server works because another client can connect just fine and the client seems to have done that. What causes the server to not see this incoming connection? I'm at the end of my wits here.

And I haven't been used to programming on Mac, so thank you so much for your patience if I have made some foolish mistakes.

My code is as follows:

server.cpp

    using namespace std;
    #include<iostream>
    #include <netinet/in.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h> 
    #include<arpa/inet.h>

    #define PORT 8888
    #define BACKLOG 20
    //#define DEST_IP "127.0.0.1"


    int process_conn_server(int s)
    {
        ssize_t size =0;
        char buffer[1024];

        for( ; ; )
        {
            size = read(s,buffer,1024);
           开发者_如何学编程 if(size == 0)
            {
                return 0;
            }
        }
        sprintf(buffer, "%d bytes altogether\n", (int)size);
        write(s, buffer,strlen(buffer)+1);



        return 0;
    }

    int main(int argc,char *argv[])
    {
        //cout<<"?";
        int ss, sc, r, err;
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;
        int   opt=1; 
        pid_t pid;

        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        server_addr.sin_port = htons(PORT);

        ss = socket(AF_INET, SOCK_STREAM, 0);
        if(ss<0)
        {
            cout<<"[process infro]socket error"<<endl;
            return -1;
        }
        cout<<"[process infro]socket successful"<<endl;

        r = setsockopt(ss, SOL_SOCKET,SO_REUSEADDR, (void*)&opt,sizeof(opt));
        if (r == -1)
        { 
            perror("setsockopt(listen)"); 
            return 0;
        }
        cout<<"[process infro]sockopt successful"<<endl;

        cout<<"?";
        err = bind(ss, (struct sockaddr*) &server_addr, sizeof( server_addr));
        cout<<"err";
        if(err < 0)
        {
            cout<<"[process infro]bind error"<<endl;
            return -1;
        }
        cout<<"[process infro]bind successful";


        err=listen(ss, BACKLOG);
        if(err <0)
        {
            cout<<"[process infro]listen error"<<endl;
            return -1;
        }
        cout<<"[process infro]lisen successful";

        for( ; ; )
        {
            int addrlen = sizeof(struct sockaddr);

            sc = accept(ss, (struct sockaddr*)&client_addr, (socklen_t *)&addrlen);

            if(sc < 0)
            {
                continue;
            }

            pid =  fork();
            if (pid == 0)
            {
                close(ss);
                process_conn_server(sc);
            }
            else
            {
                close(sc);
            }
        }
        //opt=0; 

        //setsockopt(ss,SOL_SOCKET,SO_REUSEADDR,(void*)&opt,sizeof(len));
    }

client.cpp

    using namespace std;
    #include<iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <time.h>
    #include <arpa/inet.h>
    #include <fstream.h>

    #define PORT 8888
    #define DEST_IP "127.0.0.1"

    void process_conn_client(int s)
    {
        ssize_t size = 0;
        char buffer[1024];

        //read from the file to be sent
        fstream outfile("programm.txt",ios::in|ios::out);       

        if (outfile.fail())
        {
            printf("[process infro]cannot open the file to be sent\n");
            return ;
        }   
        printf("[process infro]successfully open the file to be sent\n");

        while(!outfile.eof())
        {

            outfile.getline(buffer,1025,'\n');
            write(s,buffer,1024);       
            size = read(s, buffer, 1024);
            if(size = 0)
            {
                return ;
            }
            //write to the server
            write(s,buffer,size);

            //get response from the server
            size=read(s,buffer,1024);
            write(1,buffer,size);

        }
        outfile.close();    //关闭文件
    }

    int main(int argc,char *argv[])
    {
        int s;
        struct sockaddr_in server_addr;
        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = inet_addr(DEST_IP);
        server_addr.sin_port =  htons(PORT);

        s = socket(AF_INET, SOCK_STREAM, 0);
        if(s < 0)
        {
            cout<<"[process infro]socke error"<<endl;
            return -1;
        }
        cout<<"[process infro] socket built successfully\n";



        //inet_pton(AF_INET, argv[1], &server_addr.sin_addr);


        connect(s, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
        cout<<"[process infor] connected\n";
        process_conn_client(s);

        close(s);

        return 0;
    }


This may be unrelated.... but it won't fit in a comment...

In your server you do this:

    int process_conn_server(int s)
    {
        ssize_t size =0;
        char buffer[1024];

        for( ; ; )
        {

// keep reading until read returns 0

            size = read(s,buffer,1024);
            if(size == 0)
            {
                return 0;
            }
        }
        sprintf(buffer, "%d bytes altogether\n", (int)size);
        write(s, buffer,strlen(buffer)+1);



        return 0;
    }

In your client you do this:

    void process_conn_client(int s)
    {
        ssize_t size = 0;
        char buffer[1024];

        //read from the file to be sent
        fstream outfile("programm.txt",ios::in|ios::out);       

        if (outfile.fail())
        {
            printf("[process infro]cannot open the file to be sent\n");
            return ;
        }   
        printf("[process infro]successfully open the file to be sent\n");

        while(!outfile.eof())
        {

            outfile.getline(buffer,1025,'\n');

// write to server?

            write(s,buffer,1024);       

// read from server?

            size = read(s, buffer, 1024);
            if(size = 0)
            {
                return ;
            }
            //write to the server
            write(s,buffer,size);

            //get response from the server
            size=read(s,buffer,1024);
            write(1,buffer,size);

        }
        outfile.close();    
    }

It's a bit hard to follow because of your variable names, but it looks like your client is working under the assumption that your server will send back a response for every chunk of data received, which isn't the case. You server doesn't appear to have changed the accepted socket to non-blocking, so it's going to block on the read call until there is some data to read (it's never going to get 0)...

Are you sure it's failing before this point? Do you have some sample output?

Aso, in your call to accept, you pass addrlen...

 int addrlen = sizeof(struct sockaddr);  

I think this should be:

 int addrlen = sizeof(struct sockaddr_in);   /* sizeof(client_addr) */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜