In Windows platform I can check if a socket is connected by this code:
bool isConnected(int socket)
{
int err;
int len = sizeof (err);
getsockopt(socket, SOL_SOCKET, SO_ERROR, (char*)&err, &len);
return err == 0;
}
In OS X this function always returns true.
What is the official way to do it?
This function works for both Windows and Mac platforms:
bool isConnected(int socket)
{
int err;
#ifdef __APPLE__
uint32_t len;
#elif _Windows
int len;
#endif
sockaddr_in addr;
len = sizeof (addr);
err = getpeername(socket, (sockaddr*)&addr, &len);
return err == 0;
}
The only way I know to determine if a blocking socket is connected is to perform an actual send/recv operation and check the result for error. On Windows, a non-blocking socket can also issue asynchronous notifications on socket activity, like FD_CLOSE for disconnects.
The one place SOL_ERROR comes in handy is for a non-blocking connect() call. After select() reports the connect operation is finished, SOL_ERROR can be used to determine whether connect() was successful or why it failed.
This - if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) is close enough to winsock's isConnected.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
register int s;
register int bytes;
struct sockaddr_in sa;
char buffer[BUFSIZ+1];
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return 1;
}
bzero(&sa, sizeof sa);
sa.sin_family = AF_INET;
sa.sin_port = htons(13);
sa.sin_addr.s_addr = htonl((((((192 << 8) | 43) << 8) | 244) << 8) | 18);
if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
perror("connect");
close(s);
return 2;
}
while ((bytes = read(s, buffer, BUFSIZ)) > 0)
write(1, buffer, bytes);
close(s);
return 0;
}
Related
I am writing a set of tests that depend on two applications communicating (locally) with a UDP socket. These UDP sockets are originally bound using port 0, then the actual bound port is queried using getsockname and shared between the two applications. Occasionally, the subsequent recv calls fail to return any data without any error reporting during binding etc.
I've simplified my implementation to a bare-bones test an included it below. It fails with "Received failed, got -1 expeccted 6" about 1 of 10 runs. What am I missing in the socket setup to reliable use an ephemeral, OS-assigned port?
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <thread>
int32_t createSocket(uint16_t& rBoundPort)
{
int32_t s = socket(AF_INET, SOCK_DGRAM, 0);
if (s >= 0)
{
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeval)) < 0)
{
::close(s);
return -1;
}
int32_t r(1);
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(int)) < 0)
{
close(s);
return -1;
}
int32_t bufSize(50000);
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize)) < 0)
{
close(s);
return -1;
}
// Setup local listening port
sockaddr_in listenAddress;
memset(reinterpret_cast<char*>(&listenAddress), 0, sizeof(listenAddress));
listenAddress.sin_family = AF_INET;
inet_aton("127.0.0.1", &listenAddress.sin_addr);
listenAddress.sin_port = htons(0);
if (bind(s, (struct sockaddr*)&listenAddress, sizeof(listenAddress)) != 0)
{
close(s);
return -1;
}
// Update the bound listen port
socklen_t boundAddrLen = sizeof(sockaddr_in);
getsockname(s, (struct sockaddr*)&listenAddress, &boundAddrLen);
rBoundPort = ntohs(listenAddress.sin_port);
}
return s;
}
void mysockettest(int32_t s, uint16_t destPort)
{
const int32_t dataSize = 6;
char aWrite[dataSize] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char aRx[dataSize];
sockaddr_in dest;
memset((char*)&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
inet_aton("127.0.0.1", &dest.sin_addr);
dest.sin_port = htons(destPort);
sendto(s, aWrite, dataSize, 0, (struct sockaddr*)&dest, sizeof(sockaddr_in));
sockaddr_in src;
socklen_t srcSize = sizeof(sockaddr_in);
memset((char*)&src, 0, srcSize);
int32_t bytesReceived = recvfrom(s, aRx, dataSize, 0, (struct sockaddr*)&src, &srcSize);
if (bytesReceived != dataSize)
{
printf("Received failed, got %d expected %d\n", bytesReceived, dataSize);
}
}
int main(int argc, char** pargv)
{
uint16_t s1Port(0);
int32_t s1 = createSocket(s1Port);
if (s1 < 0)
{
printf("FAILED TO OPEN SOCKET 1\n");
return -1;
}
if (s1Port < 1)
{
printf("FAILED TO BIND SOCKET 1 TO PORT\n");
return -1;
}
uint16_t s2Port(0);
int32_t s2 = createSocket(s2Port);
if (s2 < 0)
{
printf("FAILED TOOPEN SOCKE 2\n");
}
if (s2Port < 1)
{
printf("FAILED TO BIND SOCKET 2 TO PORT\n");
}
std::thread t1(mysockettest, s1, s2Port);
std::thread t2(mysockettest, s2, s1Port);
t1.join();
t2.join();
close(s1);
close(s2);
}
```
Unforntunately I can't explain the details of they "why" (perhaps someone else can), but I found that adding a global mutex lock to protect the send corrected the instability on the send. The global lock isn't practical for my situation, so instead I implemeneted a simply retry loop on the send (if send() == -1, retry 10 times). While this solution is not ideal, it has proven stable.
Once that stability was resolved, I also saw occasional failures on the read side. With SO_REUSEADDR and binding with port 0 (OS-assignment), it was possible to have both sockets be assigned and bind to the same port, so I removed the SO_REUSEADDR when binding to port 0.
I am learning linux socket programming, I expect that server can read data, even I add a delay but it just drops the buffer data, and receive the recent data, that is why, Thanks. The code has been presented.
By the way, Could you show a common practice to deal with this kind of situation?
Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
const char hello[] = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
for (int i = 0;; i++)
{
sleep(5);
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
}
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
const char data[] = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
for (int i = 0;; i++)
{
sleep(1);
std::string hello = std::string(data) + std::to_string(i);
if (send(sock, hello.c_str(), hello.length() + 1, 0) != hello.length() + 1)
{
printf("error send %d \n", i);
}
printf("Hello message sent %d\n", i);
}
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);
return 0;
}
The problem is, that the messages get concatenated in the socket. The socket represents a byte stream. Your sender puts bytes into the stream every second. On the first iteration, it writes "Hello from client0\0" (19 bytes) to the stream.
After one second, it writes "Hello from client1\0", and then "Hello from client2\0", "Hello from client3\0" and "Hello from client4\0", Now, after 5 Seconds, 5*19 = 95 bytes are written to the byte stream.
Now, the receiver calls valread = read(new_socket, buffer, 1024);. Guess what, it reads all 95 bytes (because you specified 1024 as buffer size) and sets valread to 95.
Then you call printf("%s\n", buffer);, which only prints the first 18 bytes of buffer, because there is a '\0' as 19th byte, which terminates '%s' format. Allthough 95 bytes are received, 76 bytes are missing in the output of your program.
If you use '\n' instead of '\0' as message separator and use write(1, buffer, valread) instead of printf("%s\n") on the receiving side, you will see all your data.
std::string hello = std::string(data) + std::to_string(i) + "\n";
if (send(sock, hello.c_str(), hello.length(), 0) != hello.length()) ...
Conclusion:
Stream sockets realize byte sreams, the do not preserve message boundaries.
If message bounaries must be preserved, you need to use a protocol on top of the stream to mark your message boundaries. The proptocol could be as simple as using '\n' as a message seaparator, as long as '\n' is not part of your message payload (e.g. when unsign a simple text protocol).
You block the server for 5 seconds and it cannot receive some messages from the client.
for (int i = 0;; i++)
{
sleep(5);
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
}
How can a client check if the server is receiving a message? I think this was discussed in Linux socket: How to make send() wait for recv()
P.S. It looks like there is a synchronizing piece of code, but you pulled it out of the loop.
Server:
}
send(new_socket, hello, strlen(hello), 0);
Client:
}
valread = read(sock, buffer, 1024);
I am trying to get a very basic hello world UDP sender and UDP multicast listener to work. I have a PC but have a virtual machine with the Linux OS CentOS. It has no problems connecting to the internet. The sender and listener are two separate programs, Eclipse is my environment.
The Sender...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1" // 127.0.0.1
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd;
struct ip_mreq mreq;
char *message="Hello, World!";
int message_size = strlen(message) + 1;
// Create a UDP socket
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
perror("socket(...) ");
return -1;
}
// allow multiple sockets to use the same PORT number
u_int reuse_port = 1;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("setsockopt(...) ");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(UDP_GROUP);
addr.sin_port = htons(UDP_PORT);
printf("Begin sendto(...) infinite loop\n");
while (true)
{
printf("Sending message: %s, of size: %d\n", message, message_size);
if (sendto(fd, message, message_size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("sendto(...): ");
return -1;
}
// printf("message sent: %s\n", message);
sleep(1);
}
return 1;
}
The Listener...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1"
#define MAX_BUFFER_SIZE 256
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, nbytes;
socklen_t addrlen;
struct ip_mreq mreq;
char msgbuf[MAX_BUFFER_SIZE];
u_int reuse_port = 1;
// Create a socket
fd = socket(AF_INET,SOCK_DGRAM,0);
if (fd < 0)
{
perror("create socket failed");
return -1;
}
// allow multiple sockets to use the same PORT number
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("Reusing port number failed");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(UDP_PORT);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return -1;
}
// Set the recvfrom timeout after 1 s
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
perror("Error setting recvfrom timeout\n");
return -1;
}
// use setsockopt() to request that the kernel join a multicast group
mreq.imr_multiaddr.s_addr = inet_addr(UDP_GROUP);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
return -1;
}
addrlen = sizeof(addr);
printf("Begin recvfrom(...) infinite loop\n");
while (true)
{
nbytes = recvfrom(fd, msgbuf, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (nbytes < 0)
{
printf("recvfrom timeout\n");
}
else
{
printf("message received: %s\n", msgbuf);
}
}
return 1;
}
Every second, the sender program printf's "Sending message: Hello, World!, of size: 14" and every two seconds the receiver printf's "recvfrom timeout". I have set Wireshark to look at UDP traffic and I definitely see the sento data. The recvfrom is not getting any data. I have tried using many different Group IP's from 255.0.0.0 to 239.255.255.255, no change. I have tried many different ports, no change. Is their a special setup I need to do on my network card? I'm not sure what else to do. Small edit, the recvfrom and sendto message should not have "&".
For a project i need to send a UDP broadcast every second to 87.255.255.255 and port 4448 with the values of my project. I have writen some code in c++ but i got always the error:
Assertion `::bind(s, (sockaddr *)&si_me, sizeof(sockaddr))!=-1' failed
with this line:
//assert(::bind(s, (sockaddr *)&si_me, sizeof(sockaddr))!=-1);
When i delete this line the code runs but i find nothing on wireshark.
Does anyone have a solution or some extra info to build a broadcast sender?
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>
#include <string.h>
#include <ctime>
int main(int argc, char const *argv[]) {
sockaddr_in si_me, si_other;
int s;
printf("Making socket\n");
assert((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))!=-1);
fprintf(stderr,"usage %s hostname port\n", argv[0]);
int port=4448;
int broadcast=1;
setsockopt(s, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast);
memset(&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = INADDR_ANY;
assert(::bind(s, (sockaddr *)&si_me, sizeof(sockaddr))!=-1);
while(1)
{
printf("Send message to broadcast\n");
char buf[10000];
strcpy(buf, "test for wireshark");
unsigned slen=sizeof(sockaddr);
send(s, buf, sizeof(buf)-1, 0);
//recvfrom(s, buf, sizeof(buf)-1, 0, (sockaddr *)&si_other, &slen);
printf("recv: %s\n", buf);
sleep(1);
}
}
Apparently there's some weirdness with broadcasting under UNIX. So this may or may not work as expected.
void errno_abort(const char* header)
{
perror(header);
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[])
{
#define SERVERPORT 4567
struct sockaddr_in send_addr, recv_addr;
int trueflag = 1, count = 0;
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
errno_abort("socket");
#ifndef RECV_ONLY
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
&trueflag, sizeof trueflag) < 0)
errno_abort("setsockopt");
memset(&send_addr, 0, sizeof send_addr);
send_addr.sin_family = AF_INET;
send_addr.sin_port = (in_port_t) htons(SERVERPORT);
// broadcasting address for unix (?)
inet_aton("127.255.255.255", &send_addr.sin_addr);
// send_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
#endif // ! RECV_ONLY
#ifndef SEND_ONLY
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
&trueflag, sizeof trueflag) < 0)
errno_abort("setsockopt");
memset(&recv_addr, 0, sizeof recv_addr);
recv_addr.sin_family = AF_INET;
recv_addr.sin_port = (in_port_t) htons(SERVERPORT);
recv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr*) &recv_addr, sizeof recv_addr) < 0)
errno_abort("bind");
#endif // ! SEND_ONLY
while ( 1 ) {
#ifndef RECV_ONLY
char sbuf[256] = {};
snprintf(sbuf, sizeof(sbuf), "Hello(%d)!", count++);
if (sendto(fd, sbuf, strlen(sbuf)+1, 0,
(struct sockaddr*) &send_addr, sizeof send_addr) < 0)
errno_abort("send");
printf("send: %s\n", sbuf);
usleep(1000000/2);
#endif // ! RECV_ONLY
#ifndef SEND_ONLY
char rbuf[256] = {};
if (recv(fd, rbuf, sizeof(rbuf)-1, 0) < 0)
errno_abort("recv");
printf("recv: %s\n", rbuf);
#endif // ! SEND_ONLY
}
close(fd);
return 0;
}
Hope this helps. Good luck.
Sending a UDP packet with size 10000 is probably a bad idea.
Try using strlen(buffer) instead when calling send(). This might be a reason why you don't see anything on shark.
To find a reason why bind() fails, you need to eval errno.
BTW: I remember one TCP stack implementation, which did not like IPPROTO_UDP as third parameter to the socket() call even though it is supposed to work according to the standard. Try using 0 instead.
i have been trying to find how to send a file in chunks in C or C++
i looked at some examples in here did not find good example. i am very new to sockect programming in C/C++
http://beej.us/guide/bgnet/html/single/bgnet.html
any ideas how i need to send files in chunk between client and server? client requesting the file, server sending it back.
i found this for send but not sure about receiving it.
#include <sys/types.h>
#include <sys/socket.h>
int sendall(int s, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
I just wrote this code for receiving files in Client using linux sockets in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 4118
#define MaxBufferLength 1024 // set the size of data you want to recieve from Server
int main()
{
int sockFd, bytesRead= 1, bytesSent;
char buffer[MaxBufferLength];
struct sockaddr_in server, client;
server.sin_port= htons(PORT);
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
sockFd = socket(AF_INET, SOCK_STREAM, 0);
if(sockFd < 0)
printf("Unable to open socket\n");
int connectionSocket = connect(sockFd, (struct sockaddr *) &server, sizeof(struct sockaddr) );
if(connectionSocket < 0)
perror("connection not established\n");
int fd = open("helloworlds.txt",O_CREAT | O_WRONLY,S_IRUSR | S_IWUSR);
if(fd == -1)
perror("couldn't openf iel");
while(bytesRead > 0)
{
bytesRead = recv(sockFd, buffer, MaxBufferLength, 0);
if(bytesRead == 0)
{
break;
}
printf("bytes read %d\n", bytesRead);
printf("receivnig data\n");
bytesSent = write(fd, buffer, bytesRead);
printf("bytes written %d\n", bytesSent);
if(bytesSent < 0)
perror("Failed to send a message");
}
close(fd);
close(sockFd);
return 0;
}
Hope this helps
Take a look at TCP_CORK (man 7 tcp).
But really, except you want to become a socket/network programming expert, use a library !
Just think of your next problem: data encryption (e.g. HTTPS/SSL). Libraries care for the gory details...