Hello, 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 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 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