Hi
I am developing a chat server program.
I am not able to get client name from this line
read(connfd,info[j].name,MAXLINE);
When i remove read(connfd,no,3);
It is reading name.. Can somebody solve my problem. I need both. Please help
///////////server -
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <sys/shm.h>
-
#include "pthread.h"
-
#define MAXLINE 4096
-
#define LISTENQ 32
-
#define SERVPORT 3002
-
#define KEY IPC_PRIVATE
-
#define NO_OF_THREADS 10
-
char buff[MAXLINE];
-
char no[2];
-
int no_of_child=-1;
-
int iden[4];
-
struct cli_info
-
{
-
int read_from;
-
int write_to;
-
char name[50];
-
int myid;
-
};
-
-
struct cli_info info[NO_OF_THREADS];
-
int j=0;
-
int main(int argc,char **argv)
-
{
-
int listenfd,connfd;
-
struct sockaddr_in servaddr,cliaddr;
-
pthread_t thread[3];
-
socklen_t len;
-
pid_t childpid;
-
void str_echo();
-
void print_names();
-
if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
-
printf("\n\nError in creating socket");
-
bzero(&servaddr,sizeof(servaddr));
-
servaddr.sin_family=AF_INET;
-
servaddr.sin_port=htons(SERVPORT);
-
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
-
printf("\n\nError in bind");
-
if(listen(listenfd,LISTENQ)<0)
-
printf("\n\nError in listen");
-
for( ; ; )
-
{
-
len=sizeof(cliaddr);
-
if((connfd=iden[++no_of_child]=accept(listenfd,(struct sockaddr*)&cliaddr,&len))<0)
-
printf("Error in connect");
-
info[j].myid=connfd;
-
read(connfd,info[j].name,MAXLINE);
-
//fflush(stdin);
-
//fflush(stdout);
-
read(connfd,no,3);
-
//fflush(stdin);
-
//fflush(stdout);
-
info[j].write_to=atoi(no);
-
//write(connfd,info[j].name,MAXLINE);
-
// print_names(connfd);
-
pthread_create(&thread[j],NULL,(void *)&str_echo,(void *)&info[j]);
-
j++;
-
//exit(0);
-
//str_echo(connfd);
-
}
-
}
-
/*void print_names(int connfd)
-
{
-
int i=0;
-
for(i=0;i<j;i++)
-
write(connfd,"Hanji",MAXLINE);
-
}*/
-
void str_echo(void *arg)
-
{
-
struct cli_info *my_data;
-
my_data=(struct cli_info*)arg;
-
int i;
-
int writeid=info[my_data->write_to].myid;
-
// char currname[55];
-
// strcpy(currname,my_data->name);
-
// strcpy(currname,": ");
-
-
void clear_buffer();
-
clear_buffer();
-
for( ; ; )
-
{
-
if(read(my_data->myid,buff,MAXLINE)==0)
-
{
-
printf("\n\nError in reading");
-
}
-
//write(writeid,currname,strlen(currname));
-
//fflush(stdin);
-
//fflush(stdout);
-
write(writeid,buff,MAXLINE);
-
clear_buffer();
-
}
-
-
}
-
void clear_buffer()
-
{
-
int i;
-
for(i=0;i<MAXLINE;i++)
-
{
-
buff[i]='\0';
-
}
-
}
-
//////////////Client -
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <sys/select.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#define MAXLINE 4096
-
#define LISTENQ 32
-
#define SERVPORT 3002
-
char send_to[MAXLINE],recv_from[MAXLINE];
-
-
int main(int argc,char **argv)
-
{
-
void echo_cli(FILE *,int);
-
int sockfd;
-
struct sockaddr_in servaddr;
-
//printf("Heelo");
-
bzero(&servaddr,sizeof(servaddr));
-
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
-
printf("\n\nError in creating socket");
-
servaddr.sin_family=AF_INET;
-
servaddr.sin_port=htons(SERVPORT);
-
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0)
-
printf("\n\nIncorrect Address");
-
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
-
printf("\n\nError in connect");
-
fputs("Enter your name ",stdout);
-
fgets(send_to,MAXLINE,stdin);
-
//fputs(send_to,stdout);
-
write(sockfd,send_to,strlen(send_to));
-
//fflush(stdin);
-
//fflush(stdout);
-
fputs("Enter the client number ",stdout);
-
fgets(send_to,MAXLINE,stdin);
-
write(sockfd,send_to,strlen(send_to));
-
//fflush(stdin);
-
//fflush(stdout);
-
-
echo_cli(stdin,sockfd);
-
close(sockfd);
-
}
-
void clear_send_buffer()
-
{
-
int i;
-
for(i=0;i<MAXLINE;i++)
-
{
-
send_to[i]='\0';
-
}
-
}
-
void clear_recv_buffer()
-
{
-
int i;
-
for(i=0;i<MAXLINE;i++)
-
{
-
recv_from[i]='\0';
-
}
-
}
-
-
/*
-
void echo_cli(FILE *fp,int sockfd)
-
{
-
int maxfdp1;
-
fd_set rset;
-
FD_ZERO(&rset);
-
while(fgets(send_to,MAXLINE,fp)!=NULL)
-
{
-
write(sockfd,send_to,strlen(send_to));
-
if(read(sockfd,recv_from,MAXLINE)==0)
-
printf("\n\nError in reading");
-
fputs(recv_from,stdout);
-
clear_buffer();
-
}
-
}*/
-
int max(int a,int b)
-
{
-
if(a>=b)
-
return a;
-
else
-
return b;
-
}
-
void echo_cli(FILE *fp,int sockfd)
-
{
-
int maxfdp1;
-
char cli_no[MAXLINE];
-
fd_set rset;
-
FD_ZERO(&rset);
-
clear_recv_buffer();
-
clear_send_buffer();
-
for( ; ; )
-
{
-
//printf("hello");
-
FD_SET(fileno(fp),&rset);
-
FD_SET(sockfd,&rset);
-
maxfdp1=max(fileno(fp),sockfd)+1;
-
select(maxfdp1,&rset,NULL,NULL,NULL);
-
if(FD_ISSET(sockfd,&rset))
-
{
-
if(read(sockfd,recv_from,MAXLINE)==0)
-
{
-
//FD_CLR(sockfd,&rset);
-
printf("\n\nError in reading");
-
}
-
fputs(recv_from,stdout);
-
clear_recv_buffer();
-
}
-
if(FD_ISSET(fileno(fp),&rset))
-
{
-
if(fgets(send_to,MAXLINE,fp)!=NULL)
-
write(sockfd,send_to,strlen(send_to));
-
else
-
printf("\n\nError in reading");
-
clear_send_buffer();
-
}
-
}
-
}
-
2 2555 Banfa 9,065
Recognized Expert Moderator Expert
How do you know that it is the read(connfd,info[j].name,MAXLINE); and not the read(connfd,no,3); ?
The thing to remember is that a TCP socket is a byte stream not a packetised stream. That is just because you sent the name and then then number in 2 calls to write that is not necessarily how it will turn up at the server.
At the extreme each byte may turn up 1 at a time in separate calls to read and at the other extreme they may all turn up in a single call to read.
Suppose the name was "Client1" and the number "1". read(connfd,info[j].name,MAXLINE); attempts to read 4096 bytes, but your are sending 8, that could easily all turn up in a single read call leaving the read(connfd,no,3); hanging on data that wont arrive because it has already been read.
You can not hang you server on mimicking the same write calls the client makes with read calls. You will be doomed to failure.
The server needs to read the data as bytes (because it is a byte stream) and then parse those bytes into the field values it is expecting (a name and an ASCII formatted number). You need some way in the data to tell where the name stops and the number starts.
Thank You Banfa. I got your point. Thanks a lot. Will keep this in mind.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dariusz |
last post by:
Below is part of a code I have for a database. While the database table is
created correctly (if it doesn't exist), and data is input correctly into
the database when executed, I have a problem...
|
by: Boris Wilhelms |
last post by:
Hello all,
at first, sorry for my bad English, I’ll give my best
We have a strange problem reading Text- and VarChar-Fields.
Our configuration:
-Windows 2003 Server
-MySQL Server 3.23.36...
|
by: The_Kingpin |
last post by:
Hi again guys,
I've decided to cut my project in section and I found it way easier like
this. I'm having a little problem reading struct in a file though. I think
after this I'll be able to...
|
by: Droopy |
last post by:
Hi,
I try to implement a reusable socket class to send and receive data.
It seems to work but I have 2 problems :
1) I rely on Socket.Available to detect that the connection is closed (no...
|
by: Brad |
last post by:
I'm having a problem reading a resource stream using the following syntax:
Dim resStream As System.IO.TextReader = New _
...
| |
by: Manfred Braun |
last post by:
Hi All,
I have a problem reading queue-messages async. My QueueReader has a Start()
and a Stop() method and if my app starts, it calls Start(). The problem is,
that there are possibly several...
|
by: andreas.fabri |
last post by:
I have a problem reading integers separated by commas with VC8
This program:
___________________________
// read.C
#include <iostream>
int main()
|
by: Carl |
last post by:
Recently I got a problem with LINUX C socket programming.
I want to get how many clients are waiting for being accepted in the
socket server pending queue .
I'm trying to find the answer from the...
|
by: george585 |
last post by:
Hello!
I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
|
by: Can2002 |
last post by:
I've been trying to put together an application to change channel on a
media streaming server. The server is able to issue IR commands to
its attached equipment using LIRC, with commands being...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |