Server doesn't receive multiple write from client [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The following code is simplified.I have a server with a loop:
while(1){
fd_c = accept(fd_skt, NULL, 0);
reading = read(fd_c, buffer, 1024);
writen(fd_c, send_ok, msg_length);
}
and a client with a library which contains a socket and two functions:
int fd_skt = -1;
int connection(){
fd_skt = socket(AF_UNIX, SOCK_STREAM, 0)
connect(fd_skt, (struct sockaddr *) &skt_address, sizeof(skt_address))
writen("hello");
readn();
}
int send_another_message(){
if(fd_skt < 0)
return error(ERR_SKT_NOT_READY);
writen("I am Bob");
readn();
}
If I do this in my client the server will receives two "hello":
connection();
connection();
but if i do this in my client the server will only receive "hello" and not "I am Bob":
connection();
send_another_message();
the server doesn't receive messages.
When I use send_another_message, I don't establish a new connection (because it was previously connected with previous function call).
I can post the entire code if need.

Your server reads and writes only once on the connection, then the next loop iteration calls accept again, which will either block until there is a new connection or return an error value. In either case you overwrite your file descriptor fd_c. Therefore the server is no longer able to communicate with the old connection.
Accept the connection once, and keep the file descriptor around until the connection is closed, so you can reuse it for calls to read and write.

Related

bind failed: Address already in use on a simple code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi i'm following this simple tutorial (from https://www.geeksforgeeks.org/udp-server-client-implementation-c/ ) to create a udp client and server . But i got some issue indeed i always have this error : bind failed: Address already in use
I have already changed the port and granted permission but the error is still there.
Why does this error occur?
After your socket call and before your bind call, you need to set up the socket to reuse the address (e.g.):
// Configure server socket
int enable = 1;
// This allows you to avoid: 'Bind: Address Already in Use' error
int ret = setsockopt(server_sockd, SOL_SOCKET, SO_REUSEADDR,
&enable, sizeof(enable));
See man 7 socket for details.

TCP: Socket send/recv order [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am wondering if you need to set up the server and client sockets so that they always go
send recv send recv ...
Because I am getting an issue where I send a message, and then the initial send() receives it twice.
I send the message upload foo.c
Server displays: Message received: upload foo.c
But then the server prints the actual file contents, which should have been passed to another recv() socket call (since only the first socket in the while loop has it's contents printed)
Message received: This is some text from
the file foo.c
text hello ending
So I get the feeling it's "overflowing" into the next recv iteration.
I'm guessing you use TCP? Then you have to remember that TCP is a streaming protocol, without message boundaries and without any start or end (except connection established and closed).
A single recv call may receive less or more than what was sent in a single send call.
You need to come up with a higher-level protocol which incorporates message boundaries explicitly, for example by sending the length of the data to be received. Then you have to use loops to receive the correct amount of bytes.

client server using TCP in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am working on client server project where I have to search data requested by client from a file that is on the server side.
My code on client side:
printf("Enter data to search: \n");
fgets(buf,sizeof(buf),stdin);
send(s,buf, strlen(buf),0);
printf("Result of your search: ");
if(len = recv(s, buf, sizeof(buf),0)>0)
printf("\nMessage Received From Server -\n %s\n",buf);
my code on server side:
fp=fopen("courses.txt","r");
len=recv(new_s,buf,sizeof(buf),0);
char temp[256],tmp[512];
char *search;
while(fgets(tmp, 512, fp)!=NULL)
{
search= strstr(tmp, buf);
if(search)
{
send(new_s,tmp,strlen(tmp),0);
}
}
The strstr() always returns a null value therefore it never enters the if statement.
TCP is not a message protocol. If you want to send and receive messages (which your query is) you need a message protocol. Start by defining precisely how messages are bounded (at the byte level) and then write code to send and receive a message.
Also, don't ignore the return value of recv. How do you know how many bytes you received? And don't use functions like strlen and strcpy for anything but a C-style string. For convenience, you can make your send and receive message functions take and return C-style strings. But don't assume data on the wire will be a C-style string until your code makes it one.

Linux Socket System call "accept" never returning? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm running into a strange issue trying to test a simple socket program. When I call the "accept" function here, my program seems to hang... it prints out "SENPAI PLS" but never "SADDASSDA".
I was getting past this part of my code last night. For context, this is running on a large server with quite a few other students probably trying to do the same project as me, and I'm sure some of them are leaving their server programs running.
Could the service being busy or full cause accept to just never finish?
do{
printf("SENPAI PLS\n");
clientFD=accept(serverFD, (struct sockaddr *) &clientAddress, &clientAddressSize);
printf("SADDASSDA\n");
if(clientFD==-1){
sleep(1);
}
}while (clientFD==-1);
accept will not return until a connection is accepted (unless the listening socket is in nonblocking mode).

How i can deny data flowing over a established TCP connection but socket connection should remain in valid state. [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need a case where established TCP connection give some errors , like either sendto() failed or recieve() but socket connection should remain in place.
this way i want to check if in my application any data sending and recieving failes for one or twice , then how it will behave.
Initially, i have tested it by harcoding these values but now i want to see it in real time scenario.
Thanks in Advance.
I don't think you can make send/receive act as what you exactly think, but there may be a workaround.
You can define a global flag, and setup a signal handler to change the flag value. Then in shell you can send the signal to your app to change the flag value. By judging the flag value, your can make your program enters the error test case in real time scenario:
The global flag and the signal handler:
int link_error = 0;
static void handler(int sig)
{
link_error = 1; /* indicating error happens */
}
In main() setup a signal, such as SIGUSR1(a macro with the value 10 in LINUX X86),
struct sigaction sa = {0};
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
if(sigaction(SIGUSR1, &sa, NULL) == -1)
return -1;
Then redefine the to be tested function such as send() to judging the flag value:
int send_test(...)
{
/* Link error happens */
if(link_error) {
link_error --;
return -1;
}
return send(...);
}
When your program is running, you can do the test by kill -s 10 xxx(xxx is your program pid) at any time.
I'm not entirely sure I follow you but...
Try unplugging the network cable from the device you're talking to, not from the machine you're running your code on. It's one failure case. You could also write some test app for the other end that deliberately stalls or shuts down wr or rd only; changing the size of the tx & rx buffers for the socket will allow you to quickly fill them and see stalls as a result. You could probably also do other things like make your MTU very small, that usually tests a bunch of assumptions in code. You could also stuff something like WanEm in the mix to stress your code.
There are a lot of failure cases in networking that need testing, there's no simple answer to this.
If you get any error on a socket connection other than a read timeout, the connection is broken. It does not 'remain in place'. Ergo you cannot induce such a condition in your application. All you can do is hold up the sending end for long enough to induce read timeouts.

Resources