linux 套接字添加服务器,linux套接字:服务器在客户端之前退出

我有这个编程,只是一个简单的服务器和客户端连接的框架。我会聊聊天。

(不介意线程功能和信号......)

服务器:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#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 argumentsn");

printf("Usage: %s 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 connectionn");

} else {

printf("Accepted connection from %sn", 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..nn", 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("testn");

close(sock);

signal(SIGINT, SIG_DFL);

kill(getpid(),SIGINT);

printf("okn");

exit(0);

}

客户

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#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;

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 argumentsn");

printf("Usage: %s 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..nn", 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("testn");

close(sock);

signal(SIGINT, SIG_DFL);

kill(getpid(),SIGINT);

printf("okn");

exit(0);

}

在main()函数中,服务器只是等待连接,然后退出。

客户端,在连接休眠1秒后,然后结束。

当我运行它时,比如./server server 1234

然后./client客户端localhost 1234

两者都正常退出,但是当我第二次运行服务器时,它说无法绑定套接字:地址已在使用中。

怎么了?

服务器必须始终在客户端之后退出吗?

奖励问:我希望每个程序有两个线程,一个用于读取,一个用于写入。它们可以在同一个插座上运行吗?

非常感谢你

你可能感兴趣的:(linux,套接字添加服务器)