Questions tagged [recv]

BSD Sockets function used for receiving data from a TCP socket.

670 questions
157
votes
6 answers

How large should my recv buffer be when calling recv in the socket library

I have a few questions about the socket library in C. Here is a snippet of code I'll refer to in my questions. char recv_buffer[3000]; recv(socket, recv_buffer, 3000, 0); How do I decide how big to make recv_buffer? I'm using 3000, but it's…
adhanlon
  • 6,407
  • 13
  • 43
  • 41
88
votes
4 answers

What does Python's socket.recv() return for non-blocking sockets if no data is received until a timeout occurs?

Basically, I've read in several places that socket.recv() will return whatever it can read, or an empty string signalling that the other side has shut down (the official docs don't even mention what it returns when the connection is shut down...…
El Ninja Trepador
  • 1,013
  • 1
  • 10
  • 14
60
votes
7 answers

Python socket receive - incoming packets always have a different size

I'm using the SocketServer module for a TCP server. I'm experiencing some issue here with the recv() function, because the incoming packets always have a different size, so if I specify recv(1024) (I tried with a bigger value, and smaller), it gets…
n00bie
  • 761
  • 2
  • 11
  • 10
60
votes
7 answers

Passing a structure through Sockets in C

I am trying to pass whole structure from client to server or vice-versa. Let us assume my structure as follows struct temp { int a; char b; } I am using sendto and sending the address of the structure variable and receiving it on the other side…
codingfreak
  • 4,467
  • 11
  • 48
  • 60
26
votes
1 answer

How does the python socket.recv() method know that the end of the message has been reached?

Let's say I'm using 1024 as buffer size for my client socket: recv(1024) Let's assume the message the server wants to send to me consists of 2024 bytes. Only 1024 bytes can be received by my socket. What's happening to the other 1000 bytes?…
Tommy
  • 699
  • 4
  • 11
  • 26
20
votes
4 answers

How do I abort a socket.recv() from another thread in Python

I have a main thread that waits for connection. It spawns client threads that will echo the response from the client (telnet in this case). But say that I want to close down all sockets and all threads after some time, like after 1 connection. How…
Samuel Skånberg
  • 231
  • 1
  • 2
  • 5
18
votes
3 answers

socket select ()versus non-block recv

I've seen a few write-ups comparing select() with poll() or epoll(), and I've seen many guides discussing the actual usage of select() with multiple sockets. However, what I can't seem to find is a comparison to a non-blocking recv() call without…
Gren Meera
  • 181
  • 1
  • 1
  • 4
17
votes
3 answers

Setting timeout to recv function

I read from socket using recv function. I have problem when no data available for reading. My programm just stops. I found that I can set timeout using select function. But looks that timeout affects select function itself and recv that goes after…
vico
  • 17,051
  • 45
  • 159
  • 315
15
votes
2 answers

Python - converting sock.recv to string

I'm digging around with python and networking. while True: data = sock.recv(10240) This is definitely listening. But it seems to need to be converted to a text string. I've seen some people using struct.unpack(), but I'm not sure exactly how it…
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
15
votes
3 answers

C socket: recv and send all data

I would like to obtain a behavior similar to this: Server run Client run Client type a command like "help" or other Server responds appropriately go to 3 The problem is that when my function excCommand("help") run just a little text is received…
Lorenzo Cinque
  • 954
  • 1
  • 14
  • 22
14
votes
5 answers

If a nonblocking recv with MSG_PEEK succeeds, will a subsequent recv without MSG_PEEK also succeed?

Here's a simplified version of some code I'm working on: void stuff(int fd) { int ret1, ret2; char buffer[32]; ret1 = recv(fd, buffer, 32, MSG_PEEK | MSG_DONTWAIT); /* Error handling -- and EAGAIN handling -- would go here. Bail…
Michael Wolf
  • 2,179
  • 2
  • 16
  • 14
14
votes
4 answers

Get the number of bytes available in socket by 'recv' with 'MSG_PEEK' in C++

C++ has the following function to receive bytes from socket, it can check for number of bytes available with the MSG_PEEK flag. With MSG_PEEK, the returned value of 'recv' is the number of bytes available in socket: #include ssize_t…
jondinham
  • 8,271
  • 17
  • 80
  • 137
12
votes
5 answers

A more elegant way to use recv() and vector

So far, I have this code sample: ... int nbytes =0; vector buffer; buffer.resize(5000); nbytes = recv(socket, &buffer[0], buffer.size(),0); //since I want to use buffer.size() to know data length in buffer I…
Hitman_99
  • 2,252
  • 2
  • 19
  • 21
11
votes
3 answers

Linux socket: How to make send() wait for recv()

I am making a simple client-server application using TCP protocal. I Know that by default. recv() will block until the other side call a send() to this socket. But is it possible that send() block itself until the other side has recv()ed the msg…
user2151995
  • 113
  • 1
  • 1
  • 4
10
votes
4 answers

Call recv() on the same blocking socket from two threads

What happens if I have one socket, s, there is no data currently available on it, it is a blocking socket, and I call recv on it from two threads at once? Will one of the threads get the data? Will both get it? Will the 2nd call to recv return with…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1
2 3
44 45