I'm working on server-client application in IDE Momentics Tool.
This is the server part:
int sockfd, portno, a;
char *IP[16];
int server_to_client(struct AllPack allmess)
{
int portno;
struct sockaddr_in serv_addr;
struct hostent *server;
if (a < 2)
{
fprintf(stderr,"ERROR, no IP and port provided\n");
exit(1);
}
portno = atoi(IP[2]);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) error ("ERROR opening socket");
{
server = gethostbyname(IP[1]);
}
if (server == NULL)
{
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server -> h_addr, (char *)&serv_addr.sin_addr.s_addr, server -> h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) error ("ERROR connecting");
int n = write(sockfd, &allmess, sizeof(allmess));
if (n < 0) error ("ERROR writing socket");
close(sockfd);
return (EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
a = argc;
for(int i = 0; i < a; i++)
{
IP[i] = argv[i];
}
server_to_client(allmess);
return (EXIT_SUCCESS);
}
I'm trying to make portno = atoi(IP[2]); an array of ports and server = gethostbyname(IP[1]); an array of IP-addresses.
I'm starting my server part like this:
/home/Server 192.168.0.21 70003 192.168.0.22 70004
so I have to send data to several IP through several ports at the same time?
Related
I have an assignment to combine MPI and Socket programming for searching a file. MPI will be used to create processes which search into folders whereas sockets will be used for sending and receiving messages. My only problem right now is how I notify my master process if no file was found. Everything else is working fine.
Here is my code:
int main(int argc, char** argv)
{
int rank, size;
DIR *d;
FILE *fp;
struct dirent *dir;
if (argc < 2) {
error("Ju lutem jepni emrin e file.");
}
printf("Fillon kerkimi...\n\n");
// Initialize the MPI environment.
MPI_Init(&argc, &argv);
// Get the number of processes
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Get the rank of the process
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
server();
} else {
char str[100];
sprintf(str, "./%d/%s", rank, argv[1]);
//if file is found
if ((fp = fopen(str, "r")) != NULL) {
char buffer[4096];
int i=0;
char c;
while((c = getc(fp)) != EOF)
{
buffer[i++] = c;
}
buffer[i]='\0';
client(buffer);
}
}
MPI_Finalize();
return 0;
}
The server() and client() functions are used for sending and receiving using sockets. Didn't post the code because it is working fine.
Any ideas?
EDIT:
Server code:
void server() {
int sockfd, newsockfd, clilen;
char buffer[MAX_LEN];
struct sockaddr_in serv_addr, cli_addr;
int n;
struct timeval timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
}
//clear
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORTNO);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}
if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0) {;
printf("File nuk u gjend!");
return;
}
listen(sockfd,5);;
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
error("ERROR on accept");
}
bzero(buffer, 256);
if (read(newsockfd, buffer, 255) < 0)
{
error("ERROR reading from socket");
}
close(newsockfd);
close(sockfd);
printf("File output: \n%s\n", buffer);
return;
}
Client code:
void client(char* msg[]) {
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
strcpy(buffer, msg);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
}
server = gethostbyname("localhost");
if (server == NULL)
{
fprintf(stderr, "ERROR no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy( (char *) server->h_addr,
(char *) &serv_addr.sin_addr.s_addr,
server->h_length
);
serv_addr.sin_port = htons(PORTNO);
while(1)
{
if (connect(sockfd, &serv_addr, sizeof(serv_addr)) >= 0)
break;
}
if (write(sockfd, buffer, strlen(buffer)) < 0)
{
error("ERROR writing to socket");
}
close(sockfd);
return;
}
Note: I'm using setsockopt() on the server function which seems to work well for a timeout and do the job, though it doesn't let me print my own message to the console.
I am a newbie in libevent, my program below is quite simple. I want to call bubufferevent_write several times, but is seems that it works only for the 1st time.
int main(int argc, char const *argv[])
{
struct sockaddr_in serv_addr;
struct hostent *server;
int portno = atoi(argv[2]);
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
struct event_base* base = event_base_new();
evutil_socket_t fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
struct bufferevent* con_conn = bufferevent_socket_new(base,fd,BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(con_conn,NULL,NULL,NULL,NULL);
bufferevent_enable(con_conn,EV_READ|EV_WRITE|EV_PERSIST);
bufferevent_socket_connect(con_conn,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in));
int enable = 1;
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&enable, sizeof(enable)) < 0)
printf("TCP_NODELAY SETTING ERROR!\n");
char data[5] = "sss";
int i;
for (i = 0; i < 2; ++i)
{
int rc = bufferevent_write(con_conn,data,5);
printf("rc is %d\n", rc);
}
event_base_dispatch(base);
return 0;
}
My server side:
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
//exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
printf("waiting...\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
sleep(5);
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
return 0;
}
I'm writing a chat room program that communicates over network using TCP. If user provide ip address as a command line argument, the program would attempt to connect to that address. If not, server will wait for others to connect.
The server has no problem receiving whatever text message the client send. However, the client side only receives text messages from server only when it sends its own message. How do I fix that so that client side receives messages right away? This is my code
Server code:
#define MAX_CLIENTS 100
static unsigned int cli_count = 0;
static int uid = 10;
typedef struct {
struct sockaddr_in addr;
int connfd;
int uid;
char name[32];
} client_t;
client_t *clients[MAX_CLIENTS];
void queue_add(client_t *cl)
{
int i;
for(i=0;i<MAX_CLIENTS;i++)
{
if(!clients[i])
{
clients[i] = cl;
return;
}
}
}
void queue_delete(int uid)
{
int i;
for(i=0;i<MAX_CLIENTS;i++)
{
if(clients[i])
{
if(clients[i]->uid == uid)
{
clients[i] = NULL;
return;
}
}
}
}
void send_message_all(char *s)
{
int i;
for(i=0;i<MAX_CLIENTS;i++)
{
if(clients[i])
{
write(clients[i]->connfd, s, strlen(s));
}
}
}
void *hanle_client(void *arg)
{
char buff_in[256];
char buff_out[256];
int rlen;
cli_count++;
client_t *cli = (client_t *)arg;
sprintf(buff_out, "<<JOIN, HELLO %s\r\n", cli->name);
send_message_all(buff_out);
bzero(buff_in,sizeof(buff_in));
while((rlen = read( cli->connfd,buff_in,sizeof(buff_in)-1))>0)
{
sprintf(buff_out, "[%s] %s\r\n", cli->name, buff_in);
send_message_all(buff_out);
}
close(cli->connfd);
/* Delete client from queue and yeild thread */
queue_delete(cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0, portno;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
if (argc < 2) {
printf("ERROR, no port provided\n");
exit(1);
}
//Create socket
listenfd= socket(AF_INET , SOCK_STREAM , 0);
if (listenfd == -1)
{
printf("Could not create socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portno);
/* Bind */
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("Socket binding failed");
return 1;
}
/* Listen */
if(listen(listenfd, 10) < 0)
{
perror("Socket listening failed");
return 1;
}
printf("<[SERVER STARTED]>\n");
socklen_t clilen = sizeof(cli_addr);
/* Accept clients */
while( (connfd = accept(listenfd, (struct sockaddr *)&cli_addr, (socklen_t*)&clilen)))
{
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->addr = cli_addr;
cli->connfd = connfd;
cli->uid = uid++;
sprintf(cli->name, "%d", cli->uid);
/* Add client to the queue and fork thread */
queue_add(cli);
pthread_create(&tid, NULL, &hanle_client, (void*)cli);
}
}
Client code:
int main(int argc , char *argv[])
{
int sockfd, portno ;
struct sockaddr_in serv_addr;
struct hostent *server;
char message[2000],server_reply[2000];
if (argc <3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(1);
}
portno = atoi(argv[2]);
//Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
//Connect to remote server
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
exit(1);
}
puts("Connected\n");
//keep communicating with server
while(1)
{
//Receive a reply from the server
bzero(server_reply,2000);
if( recv(sockfd , server_reply , 2000,0) < 0)
{
puts("recv failed");
break;
}
printf("%s", server_reply);
server_reply[0]='\0';
//Send Message to server
printf("Enter Message:");
bzero(message,2000);
fgets(message, sizeof(message),stdin);
if(send(sockfd , message , strlen(message),0) < 0)
{
puts("Send failed");
return 0;
}
}
close(sockfd);
return 0;
}
I am not sure if I understood your problem correctly. But at a high level, I noticed that your hanleClient method calls close(cli->connfd) on the clients socket after calling sendall. After calling close, you are deleting the client details from the queue. This way, the client being deleted will never receive any future messages. Are you sure this is what you want?
Try removing these lines and check if that is what you want -
close(cli->connfd);
/* Delete client from queue and yeild thread */
queue_delete(cli->uid);
free(cli);
cli_count--;
This way, whenever the server receives a message, it will try to send it to all clients that are connected to the server.
Note: Your code is not thread safe and will result in unexpected behaviour since you are accessing global data from within threads without using mutexes.
I have written a sample socket program in C on Linux.
The server is single process server.
The program is simple where the server is running and the client connects to the server waiting on accept() call.
When the server accepts the client request it sends some string to the client using write call.
Server Code:
#define MAXHOSTNAME 256
#define MAX_CONN 10
void single_process_server(unsigned int portNumber)
{
int listenSockFd, acceptSockFd, portNo;
socklen_t clilen;
char buffer[256] = "Connected";
struct sockaddr_in srvInfo;
int n;
char sysHost[MAXHOSTNAME+1]; // Hostname of this computer we are running on
if((listenSockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(listenSockFd);
error("ERROR opening socket");
}
bzero((char *) &srvInfo, sizeof(srvInfo));
portNo = portNumber;
srvInfo.sin_family = AF_INET;
srvInfo.sin_addr.s_addr = htonl(INADDR_ANY);
srvInfo.sin_port = htons(portNo);
if (bind(listenSockFd, (struct sockaddr *) &srvInfo, sizeof(srvInfo)) < 0)
{
close(listenSockFd);
error("ERROR on binding");
}
listen(listenSockFd,5);
while(1)
{
clilen = sizeof(srvInfo);
if((acceptSockFd = accept(listenSockFd, (struct sockaddr *) &srvInfo, &clilen)) < 0)
{
error("ERROR on accept");
}
if((n = write(acceptSockFd,buffer,255)) < 0)
{
error("ERROR writing to socket");
}
close(acceptSockFd);
}
close(listenSockFd);
}
int main(int argc, char* argv[])
{
unsigned int portNo= 0;
portNo = 444;
single_process_server(portNo);
return(0);
}
The client receives the string using read call.
Client Code:
void simple_internet_client(char hostip[], unsigned int portNo)
{
int sockFd, portno, n;
struct sockaddr_in srvInfo;
struct hostent *server;
char buffer[256];
portno = portNo;
if((sockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
error("ERROR opening socket");
}
if((server = gethostbyname(hostip)) == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &srvInfo, sizeof(srvInfo));
srvInfo.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&srvInfo.sin_addr.s_addr, server->h_length);
srvInfo.sin_port = htons(portno);
if(connect(sockFd,(struct sockaddr *) &srvInfo,sizeof(srvInfo)) < 0)
error("ERROR connecting");
{
bzero(buffer,256);
if((n = read(sockFd,buffer,255)) < 0)
error("ERROR reading from socket");
printf("Socket read = %s\n", buffer);
}
close(sockFd);
}
main(int argc, char* argv[])
{
char hostip[50] = {0};
unsigned int portNo= 444;
printf("B. Provide the host name or IP:\n");
scanf("%s",&hostip);
simple_internet_client(hostip, portNo);
return(0);
}
When I run the client(./clientdemo) at some intervals then the client receives whatever string the server sends.
But when I run the client multiple times through some script then the client remains stuck up at read() call and does not receive what the server has sent.
Simply speaking when the client connects to the server at faster rate then the server writes to the socket at same rate.But the client is not able to read the data written on the socket and remains stuck up on the read() call.
What may be the cause of this and why client is not able to read data from socket at same rate as server writes to it?
when i run both client and server i get this as result:
on server
Server is on
Client: XXX.X.X.X accepted
(that's the point that the program pauses until I type ctrl+C in client)
I try to get in while
I get in while
Client
AI Choise:Scissor
AI:Wins ratio 0,Looses ratio 0,Ties ratio 1
I get in while
Client
AI Choise:Scissor
AI:Wins ratio 0,Looses ratio 0,Ties ratio 2
on client
1)--> Paper
2)--> Scissor
3)--> Rock
4)--> Quit
^C
my code:
Server:
int main(int argc, char *argv[]){
printf("\nServer is on\n");
int sockfd, newsockfd, portno;
socklen_t clilen;
int Client_Choice;
struct sockaddr_in serv_addr, cli_addr;
int n,Who_Wins;
int ai_wins=0,ai_looses=0,ties=0,total=0,ai_win_ratio=0,ai_looses_ratio=0,ai_ties_ratio;
time_t t;
srand((unsigned) time(&t));
if (argc < 2) {
fprintf(stderr,"\nERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
error("\nERROR opening socket\n",sockfd);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
error("\nERROR on binding\n",sockfd);
}
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0){
error("\nERROR on accept\n",sockfd);
}
char *cli_IP = malloc(sizeof(cli_addr.sin_addr.s_addr));
if (!cli_IP){
error("\nCould not allocate memory for conversion.\n",sockfd);
}
inet_ntop( AF_INET , &cli_addr.sin_addr.s_addr , cli_IP , INET_ADDRSTRLEN );
printf("\nClient: %s accepted\n",cli_IP);
printf("I try to get in while");
while(Client_Choice!=4){
printf("I get in while");
n= read( newsockfd, &Client_Choice, sizeof(Client_Choice) );
if(n < 0) {
error("\nERROR reading from socket\n",sockfd);
}
.
.
.
}
close(newsockfd);
close(sockfd);
return 0;
}
client
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int send;
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("ERROR opening socket\n");
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
error("ERROR connecting\n");
}
while(send < 1 ||send > 4){
printf("\n\t1)--> Paper\n\t2)--> Scissor\n\t3)--> Rock\n\t4)--> Quit\n");
scanf("%d",&send);
n = write(sockfd,&send,sizeof(send));
if (n < 0){
error("ERROR writing to socket\n");
}
n = read(sockfd,&send,sizeof(send));
if (n == 0){
error("ERROR reading from socket\n");
}
}
close(sockfd);
return 0;
}
Your server is blocked waiting the message from the client. You have not reserved resources for dealing with several clients and only have a single process serving a single client (using the connection socket from accept(2)). That's the reason of it to appear blocking. In a normal server scenario, a new process is spawned by a fork(2) system call to deal with the socket obtained from accept(2) while the main process continues to accept(2) connections on the socket used for accepting new connections. As you are dealing with two sockets in server, but not attending the socket descriptor where accept(2) connections come in, it appears to be blocked, but it is actually ready to accept the commands from the active client. This is what is called a sequential server (it doesn't allow a connection before the first one terminates).
By the way, send is uninitialized before use in first while (send < 1 || send > 4) sentence so in case you get it casually equal to 2 (for example) you won't get client code into the while at all. This is only a point, probably there will be more. Why have you used different names for the message type interchanged between server and client? this makes more difficult to search for errors.