开发者

linux sockets: server exits before client

开发者 https://www.devze.com 2023-01-05 08:28 出处:网络
I have this prog, just a skeleton for a simple sever and client connection. I will make it a chat. (dont mind the thread func and signals..)

I have this prog, just a skeleton for a simple sever and client connection. I will make it a chat. (dont mind the thread func and signals..)

server:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>


#define BUFFERSIZE 512
#define TIMESIZE 32
#define QUIT "!quit"


// thread pou diavazei
void *readthread(void *argp);
// katharizei ligo prin kleisei to programma
void progreset();
// kleinei to prog me ctrl-c
void sigexit();


int sock, endchat;
char username1[50];
pthread_t thrread;


int main(int argc, char** argv) {
    int port, s;
    char username[50];
    struct sockaddr_in server, client;
    struct sockaddr *serverptr, *clientptr;
    unsigned int clientlen;
    char buf[BUFFERSIZE];
    int len;
    time_t sec;
    char timestr[TIMESIZE];


    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, sigexit);


    if (argc != 3) {
        printf("Error: Wrong arguments\n");
        printf("Usage: %s <username> <port>\n", argv[0]);
        return -1;
    }

    strcpy(username, argv[1]);
    port = atoi(argv[2]);

    // ftiaxno to socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Failed to create socket");
        return -1;
    }
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons((short)port);
    if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
        perror("Failed to bind socket");
        return -1;
    }

    if (listen(s, 5) != 0) {
        perror("Error in listen()");
        return -1;
    }

    clientptr = (struct sockaddr *)&client;
    clientlen = sizeof(client);

    // perimeno sindesi apo ton pelati
    printf("Accepting connections on port %d..\n", port);
    if ((sock = accept(s, clientptr, &clientlen)) < 0) {
        perror("Error in accept()");
        return -1;
    }

    // pairno to ip tou pelati
    if (getpeername(sock, (struct sockaddr *)&client, &clientlen) < 0) {
        printf("Accepted connection\n");
    } else {
        printf("Accepted connection from %s\n", inet_ntoa(client.sin_addr));
    }

    // stelno kai pairno ta usernames
    bzero(buf, sizeof(buf));
    strcpy(buf, username);
    if (write(sock, buf, sizeof(buf)) < 0) {
        perror("write1");
        return -1;
    }

    bzero(buf, sizeof(buf));
    if (read(sock, buf, sizeof(buf)) < 0) {
        perror("read1");
        return -1;
    }
    strcpy(username1, buf);

    printf("Chatting with %s..\n\n", username1);








    progreset();
    return 0;
}


void *readthread(void *argp) {
    char buf[BUFFERSIZE];
    char timestr[TIMESIZE];
    int len;
    time_t sec;
    struct tm *timeinfo;


    while (1) {

    }



    endchat = 1;
    pthread_exit(0);
}


void progreset() {
    printf("\nExiting..\n");
    close(sock);
}


void sigexit() {
    printf("test\n");
    close(sock);
    signal(SIGINT, SIG_DFL);
    kill(getpid(),SIGINT);
    printf("ok\n");

    exit(0);
}

client

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


#define BUFFERSIZE 512
#define TIMESIZE 32
#define QUI开发者_如何学PythonT "!quit"


// thread pou diavazei
void *readthread(void *argp);
// katharizei ligo prin kleisei to programma
void progreset();
// kleinei to prog me ctrl-c
void sigexit();


int sock, endchat;
char username1[50];
pthread_t thrread;


int main(int argc, char** argv) {
    int port;
    char username[50];
    struct sockaddr_in server, client;
    struct sockaddr *serverptr, *clientptr;
    unsigned int serverlen;
    struct hostent *rem;
    char buf[BUFFERSIZE];
    int len;
    time_t sec;
    char timestr[TIMESIZE];


    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, sigexit);


    if (argc != 4) {
        printf("Error: Wrong arguments\n");
        printf("Usage: %s <username> <ip address> <port>\n", argv[0]);
        return -1;
    }

    strcpy(username, argv[1]);
    port = atoi(argv[3]);

    // ftiaxno to socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Failed to create socket");
        return -1;
    }
    rem = gethostbyname(argv[2]);
    server.sin_family = AF_INET;
    bcopy((char*)rem->h_addr, (char*)&server.sin_addr, rem->h_length);
    server.sin_port = htons((short)port);
    serverptr = (struct sockaddr *)&server;
    serverlen = sizeof(server);

    // kano connect me to server
    if (connect(sock, serverptr, serverlen) < 0) {
        perror("Failed to connect");
        return -1;
    }

    // pairno kai stelno ta usernames
    bzero(buf, sizeof(buf));
    if (read(sock, buf, sizeof(buf)) < 0) {
        perror("read1");
        return -1;
    }
    strcpy(username1, buf);

    bzero(buf, sizeof(buf));
    strcpy(buf, username);
    if (write(sock, buf, sizeof(buf)) < 0) {
        perror("write1");
        return -1;
    }

    printf("Chatting with %s..\n\n", username1);







    sleep(1);










    progreset();
    return 0;
}


void *readthread(void *argp) {
    char buf[BUFFERSIZE];
    char timestr[TIMESIZE];
    int len;
    time_t sec;
    struct tm *timeinfo;

    while (1) {
    }
    endchat = 1;
    pthread_exit(0);
}


void progreset() {
    printf("\nExiting..\n");
    close(sock);
}


void sigexit() {
    printf("test\n");
    close(sock);
    signal(SIGINT, SIG_DFL);
    kill(getpid(),SIGINT);
    printf("ok\n");

    exit(0);
}

in the main() func, the server just waits for connection, and then exits. the client, after the connection sleeps for 1 sec, and then ends.

when I run this, like ./server server 1234 and then ./client client localhost 1234 both exit normally, but when i run the server the second time, it says Failed to bind socket: Address already in use.

what is wrong? must the server exit always after the client?

bonus Q: I want to have two threads per program, one to read and one to write. Can they operate on the same socket?

thank you very much


Make sure you call closesocket(s) on the socket before you exit.
Also try setsockopt SO_REUSEADDR before you open.

Here is a good tutorial on Linux Sockets.

Of course if you posted some code it would make it easier.

You can read and write on the same socket.

I suspect your getting "Address in use error" (EADDRINUSE)
You can use the bind API function to bind an address (an interface and a port) to a socket endpoint. You can use this function in a server setting to restrict the interfaces from which incoming connections are possible. You can also use this function from a client setting to restrict the interface that should be used for an outgoing connection.
The most common use of bind is to associate a port number with a server and use the wildcard address (INADDR_ANY), which allows any interface to be used for incoming connections.
The problem commonly encountered with bind is attempting to bind a port that's already in use. The pitfall is that no active socket may exist, but binding to the port is still disallowed (bind returns EADDRINUSE), which is caused by the TCP socket TIME_WAIT state. This state keeps a socket around for two to four minutes after its close.
After the TIME_WAIT state has exited, the socket is removed, and the address can be rebound without issue.
Waiting for TIME_WAIT to finish can be annoying, especially if you're developing a socket server and you need to stop the server to make changes and then restart it. Luckily, there's a way to get around the TIME_WAIT state. You can apply the SO_REUSEADDR socket option to the socket, such that the port can be reused immediately. Prior to binding an address, I call setsockopt with the SO_REUSEADDR option. To enable address reuse, I set the integer argument (on) to 1 (otherwise, you can set it to 0 to disable address reuse).

Avoiding the "Address In Use" error using the SO_REUSEADDR socket option

int sock, ret, on;
struct sockaddr_in servaddr;

/* Create a new stream (TCP) socket */
sock = socket( AF_INET, SOCK_STREAM, 0 ):

/* Enable address reuse */
on = 1;
ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );

/* Allow connections to port 8080 from any available interface */
memset( &servaddr, 0, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
servaddr.sin_port = htons( 45000 );

/* Bind to the address (interface/port) */
ret = bind( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) );

After you have applied the SO_REUSEADDR socket option, the bind API function will always permit immediate reuse of the address.

0

精彩评论

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