Unix CAN socket does not wait for message - c++

I have a unix CAN socket created as below:
#include <linux/can.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
int main() {
struct can_frame _msg;
struct sockaddr_can addr;
struct ifreq ifr;
int _sock_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (_sock_fd < 0) {
}
strcpy(ifr.ifr_name, "can0");
if (0 != ioctl(_sock_fd, SIOCGIFINDEX, &ifr)) {
}
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
fcntl(_sock_fd, F_SETFL, O_NONBLOCK);
if (0 != bind(_sock_fd, (struct sockaddr*)&addr, sizeof(addr))) {
}
while (1) {
ssize_t size = 0;
ssize_t tmp = 0;
while (size < sizeof(struct can_frame)) {
tmp = read(_sock_fd, &_msg, sizeof(struct can_frame));
if (tmp <= 0) {
printf("0\n");
}
size += tmp;
}
printf("%x %x %lx\n", _msg.can_id, _msg.can_dlc, *((uint64_t*)_msg.data));
}
return 0;
}
When I do read on this socket, i always get message immediately even if sender does not send anything. read always read duplicate of latest recieved message instead of waiting for next. I perform reading as below:
size_t size = 0;
while (size < sizeof(struct can_frame)) size += read(_sock_fd, &_msg, sizeof(struct can_frame));
It is confusing for me, why read always returns duplicated message intead of waiting for next one. If I call read function in loop with yeld, it spawns millions of duplicates of same message when sender sent just one: candump utility shows received messages right way. Why does it have this behavior?

Related

C++ non-blocking send/recv work only on one side(client or server)

I'm implementing a server in C++ with non-blocking sockets.
Since I want to send messages between the client & server, I wrote 2 wrappers around send/recv syscalls. Mainly, I want to prepend 4Bytes (message length) to every message, so that the receiver knows how long to execute recv.
Moreover I have a client/server programs that each start a socket and listen on localhost.
Then the client sends a random message, which the server receives.
When I try,however, to send from the server to the client, both programs halt.
I have tested the wrappers many times and they read/deliver data, but whenever I try to receive on a previously sending connection, then comes the problem.
I know that there is a memory leak in the secure_recv but I need it to pass some custom tests, which are not very well written.
The issue lies in the select, which returns a positive number, but then I never go inside the if (FD_ISSET(fd, &readset)) statement.
What am I doing wrong and how can we fix it ? Thanks a lot !
EDIT
My problem was that the sockets were blocking(busy working) at the select function. I updated the code so that there is no select in the secure_* functions. It's a much better way to first check if the socket is available for send/recv on a client/server thread level via select and then calling the secure_* functions. Question is answered for now.
client.cpp
// 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.h>
#include "util.h"
#define PORT 8080
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
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;
}
int numbytes;
size_t size = 0;
std::unique_ptr<char[]> buf = get_rand_data(size);
if ((numbytes = secure_send(sock, buf.get(), size, 0)) == -1)
{
std::cout << std::strerror(errno) << "\n";
exit(1);
}
std::cout << "Client sent : " << numbytes << "\n";
int64_t bytecount = -1;
while (1)
{
std::unique_ptr<char[]> buffer;
if ((bytecount = secure_recv(sock, buffer, 0)) <= 0)
{
if (bytecount == 0)
{
break;
}
}
std::cout << bytecount << "\n";
}
std::cout << "Client received : " << bytecount << "\n";
close(sock);
return 0;
}
server.cpp
// 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>
#include "util.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);
// 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);
}
// set the socket to non-blocking mode
fcntl(new_socket, F_SETFL, O_NONBLOCK);
int64_t bytecount = -1;
while (1) {
std::unique_ptr<char[]> buffer;
if ((bytecount = secure_recv(new_socket, buffer, 0)) <= 0) {
if (bytecount == 0) {
break;
}
}
std::cout << bytecount << "\n";
}
int numbytes;
size_t size = 0;
std::unique_ptr<char[]> buf = get_rand_data(size);
if ((numbytes = secure_send(new_socket, buf.get(), size, 0)) == -1)
{
std::cout << std::strerror(errno) << "\n";
exit(1);
}
std::cout << "Client sent : " << numbytes << "\n";
close(new_socket);
return 0;
}
util.h
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>
#include <cstring>
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <algorithm>
#include <memory>
#include <poll.h>
#include <iomanip>
/**
* It takes as arguments one char[] array of 4 or bigger size and an integer.
* It converts the integer into a byte array.
*/
void convertIntToByteArray(char *dst, int sz)
{
auto tmp = dst;
tmp[0] = (sz >> 24) & 0xFF;
tmp[1] = (sz >> 16) & 0xFF;
tmp[2] = (sz >> 8) & 0xFF;
tmp[3] = sz & 0xFF;
}
/**
* It takes as an argument a ptr to an array of size 4 or bigger and
* converts the char array into an integer.
*/
int convertByteArrayToInt(char *b)
{
return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
}
/**
* It constructs the message to be sent.
* It takes as arguments a destination char ptr, the payload (data to be sent)
* and the payload size.
* It returns the expected message format at dst ptr;
*
* |<---msg size (4 bytes)--->|<---payload (msg size bytes)--->|
*
*
*/
void construct_message(char *dst, char *payload, size_t payload_size)
{
convertIntToByteArray(dst, payload_size);
memcpy(dst + 4, payload, payload_size);
}
/**
* It returns the actual size of msg.
* Not that msg might not contain all payload data.
* The function expects at least that the msg contains the first 4 bytes that
* indicate the actual size of the payload.
*/
int get_payload_size(char *msg, size_t bytes)
{
// TODO:
return convertByteArrayToInt(msg);
}
/**
* Sends to the connection defined by the fd, a message with a payload (data) of size len bytes.
* The fd should be non-blocking socket.
*/
/**
* Receives a message from the fd (non-blocking) and stores it in buf.
*/
int secure_recv(int fd, std::unique_ptr<char[]> &buf)
{
// TODO:
int valread = 0;
int len = 0;
int _len = 4;
bool once_received = false;
std::vector<char> ptr(4);
while (_len > 0)
{
int _valread = recv(fd, ptr.data() + valread, _len, MSG_DONTWAIT);
if (_valread == 0)
{
break;
}
if (_valread > 0)
{
_len -= _valread;
valread += _valread;
}
if (!once_received && valread == 4)
{
once_received = true;
len = convertByteArrayToInt(ptr.data());
_len = len;
ptr = std::vector<char>(len);
valread = 0;
}
}
buf = std::make_unique<char[]>(len);
memcpy(buf.get(), ptr.data(), len);
return len;
}
/**
* Sends to the connection defined by the fd, a message with a payload (data) of size len bytes.
* The fd should be non-blocking socket.
*/
int secure_send(int fd, char *data, size_t len)
{
// TODO:
char ptr[len + 4];
int valsent = 0;
int _len = 4;
bool once_sent = false;
construct_message(ptr, data, len);
while (_len > 0)
{
int _valsent = send(fd, ptr + valsent, _len, MSG_DONTWAIT);
if (_valsent == 0)
{
break;
}
if (_valsent > 0)
{
_len -= _valsent;
valsent += _valsent;
}
if (!once_sent && valsent == 4)
{
once_sent = true;
_len = len;
}
}
return len;
}
Compilation via
g++ -O3 -std=c++17 -Wall -g -I../ client.cpp -o client -lpthread
Let's start with the write loop:
while (1)
{
// std::cerr << "first iteration send\n";
FD_ZERO(&writeset);
FD_SET(fd, &writeset);
if (select(fd + 1, NULL, &writeset, NULL, NULL) > 0)
{
if (FD_ISSET(fd, &writeset))
{
valsent = send(fd, ptr + valsent, _len, 0);
Oops. This loses valsent, which tracks how many bytes you've sent so far. So on your third loop, ptr + valsent will only add the number of bytes received the second time. You need to track the total number of bytes sent so far somewhere.
if (valsent <= 0)
{
break;
}
_len -= valsent;
And what if _len becomes zero? You'll still call select and even send. You probably want that while (1) to be while (_len > 0).
}
}
}
return len;
Now, onto the read loop:
if (select(fd + 1, &readset, NULL, NULL, NULL) > 0)
{
if (FD_ISSET(fd, &readset))
{
if (first_iteration)
{
recv(fd, ptr, 4, 0);
You ignore the return value of recv here. What if it's not 4?
len = convertByteArrayToInt(ptr);
buf = std::make_unique<char[]>(len);
_len = len;
first_iteration = false;
}
valread = recv(fd, buf.get() + valread, _len, 0);
if (valread <= 0)
{
break;
}
_len -= valread;
You don't leave the loop if _len is zero. You'll call select again, waiting for data that may never come.
}
}

Why are there no scm_timestamping control messages coming up?

My code consists of two programs: a TCP server and a TCP client. The goal of the project is to get timestamping for TCP working. I consulted this piece of linux documentation, and I can't seem to find anything that would indicate that my code shouldn't work. It says SO_TIMESTAMPING works with stream sockets. I'm really lost here. Or am I misunderstanding how this should work? I have never worked with linux and never done any networking, so there might be an obvious error on my part, but I don't see it.
client.cpp:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <linux/errqueue.h>
#include <linux/net_tstamp.h>
int port = 8989;
const char *address = "127.0.0.1";
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
fprintf(stderr, "\n Error : Could not create socket \n");
return 1;
}
// Enable timestamping:
int timestampingFlags = SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE;
int optRet = setsockopt(sockfd, SOL_SOCKET, SO_TIMESTAMPING, &timestampingFlags, sizeof(timestampingFlags));
if(optRet < 0)
{
printf("Unable to set socket option for timestamping");
} // OK
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if(inet_pton(AF_INET, address, &serv_addr.sin_addr)<=0)
{
fprintf(stderr, "\n inet_pton error occured\n");
return 1;
}
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr, "\n Error : Connect Failed \n");
perror(0);
return 1;
}
// Receive responses
while ((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
fprintf(stderr, "\n Error : Fputs error\n");
}
// Get and print the time stamp
struct msghdr msgh;
struct cmsghdr *cmsg;
struct scm_timestamping *timeStamp;
int flags = MSG_WAITALL | MSG_PEEK;
int recvRet = recvmsg(sockfd, &msgh, flags);
/* Receive auxiliary data in msgh */
// There are no messages here
for(cmsg = CMSG_FIRSTHDR(&msgh);
cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh, cmsg))
{
printf("A control message arrived!\n");
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_TIMESTAMPING)
{
timeStamp = (struct scm_timestamping *)CMSG_DATA(cmsg);
printf("Timestamp received: %ld.09%ld\n", timeStamp->ts[0].tv_sec, timeStamp->ts[0].tv_nsec);
break;
}
}
}
if(n < 0)
{
fprintf(stderr, "\n Read error \n");
}
return 0;
}
-server.cpp:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
// call this function to start a nanosecond-resolution timer
struct timespec timer_start()
{
struct timespec start_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
return start_time;
}
// call this function to end a timer, returning microseconds elapsed as a long
long timer_end(struct timespec start_time)
{
struct timespec end_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
long diffInNanos = (end_time.tv_sec - start_time.tv_sec) * (long)1e9 + (end_time.tv_nsec - start_time.tv_nsec);
return diffInNanos / 1000;
}
int port = 8989;
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("Now listening for a connection!\n");
listen(listenfd, 1);
// Wait for a connection from a client socket
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
printf("Connected!\n");
// Once connection is established, start sending messagess in a regular time interval
long timeBetweenSendsUS = 1000*1000;
for(struct timespec startTime = timer_start();
true;
startTime = timer_start())
{
memset(sendBuff, '0', sizeof(sendBuff));
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
long elapsedUS = timer_end(startTime);
usleep(timeBetweenSendsUS - elapsedUS);
printf("Sending message!\n");
write(connfd, sendBuff, strlen(sendBuff));
}
close(connfd);
return 0;
}
I then compile each file separately using g++ <filename> -o <filename> and run the server binary first and the client binary second while the server is running. So, to repeat my question: Why are there no control messages in the ancillary data?

my epoll server losses some connections. why?

I'd like to make an epoll server. But my code of the server losses some connections.
My client spawns 100 threads and each sends the same message. Then my server is supposed to receive and print them with counting numbers.
But the server seems like losing connections and I don't know why.
I changed EPOLL_SIZE from 50 to 200, and did backlog argument of listen() from 5 to 1000. But they didn't work.
1.server:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <memory>
#include <array>
#define BUF_SIZE 100
#define EPOLL_SIZE 200
void error_handling(const char *buf);
int main(int argc, char *argv[])
{
// Step 1. Initialization
int server_socket, client_socket;
struct sockaddr_in client_addr;
socklen_t addr_size;
int str_len, i;
char buf[BUF_SIZE];
int epfd, event_cnt;
if (argc != 2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
// Step 2. Creating a socket
server_socket = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
// Step 3. Binding the server address onto the socket created just right before.
if (bind(server_socket, (struct sockaddr*) &server_addr, sizeof(server_addr)) == -1)
error_handling("bind() error");
// Step 4. Start to listen to the socket.
if (listen(server_socket, 1000) == -1)
error_handling("listen() error");
// Step 5. Create an event poll instance.
epfd = epoll_create(EPOLL_SIZE);
auto epoll_events = (struct epoll_event*) malloc(sizeof(struct epoll_event) * EPOLL_SIZE);
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = server_socket;
// Step 6. Adding the server socket file descriptor to the event poll's control.
epoll_ctl(epfd, EPOLL_CTL_ADD, server_socket, &event);
int recv_cnt = 0;
while(true)
{
// Step 7. Wait until some event happens
event_cnt = epoll_wait(epfd, epoll_events, EPOLL_SIZE, -1);
if (event_cnt == -1)
{
puts("epoll_wait() error");
break;
}
for (i = 0; i < event_cnt; i++)
{
if (epoll_events[i].data.fd == server_socket)
{
addr_size = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &addr_size);
event.events = EPOLLIN;
event.data.fd = client_socket;
epoll_ctl(epfd, EPOLL_CTL_ADD, client_socket, &event);
//printf("Connected client: %d\n", client_socket);
}
else // client socket?
{
str_len = read(epoll_events[i].data.fd, buf, BUF_SIZE);
if (str_len == 0) // close request!
{
epoll_ctl(epfd, EPOLL_CTL_DEL, epoll_events[i].data.fd, nullptr);
close(epoll_events[i].data.fd);
printf("%d: %s\n", ++recv_cnt, buf);
//printf("closed client: %d \n", epoll_events[i].data.fd);
}
else
{
write(epoll_events[i].data.fd, buf, str_len); // echo!
}
} // end of else()
} // end of for()
} // end of while()
close(server_socket);
close(epfd);
free(epoll_events);
return EXIT_SUCCESS;
}
void error_handling(const char *buf)
{
fputs(buf, stderr);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
2.client:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <thread>
#include <vector>
#include <algorithm>
#include <mutex>
#define BUF_SIZE 100
#define EPOLL_SIZE 50
void error_handling(const char *buf);
int main(int argc, char *argv[])
{
// Step 1. Initialization
int socketfd;
if (argc != 3) {
printf("Usage : %s <ip address> <port>\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
std::vector<std::thread> cli_threads;
std::mutex wlock;
for (int i = 0; i < 100; i++) {
cli_threads.push_back(std::thread([&](const char* szIpAddr, const char* szPort) {
// Step 2. Creating a socket
socketfd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(szIpAddr);
server_addr.sin_port = htons(atoi(szPort));
// Step 3. Connecting to the server
if(connect(socketfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1)
error_handling("connect() error");
// Step 4. Writing message to the server
std::string msg = "Hey I'm a client!";
wlock.lock();
auto str_len = write(socketfd, msg.c_str(), msg.size()+1);
wlock.unlock();
close(socketfd);
}, argv[1], argv[2]));
}
std::for_each(cli_threads.begin(), cli_threads.end(),
[](std::thread &t)
{
t.join();
}
);
return EXIT_SUCCESS;
}
void error_handling(const char *buf)
{
fputs(buf, stderr);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
expected like...
1: Hey I'm a client!
...
100: Hey I'm a client!
but, the result varies, like...
1: Hey I'm a client!
...
n: Hey I'm a client!
where the n is less than 100.
You had undefined behaviour because of passing socketfd by reference to thread - std::thread([&](.... One instance of socket descriptor was being modified by all threads concurrently - it caused problems. Every thread should store its own descriptor.

What is the file transfer error in this program while doing a file transfer via Socket in the network?

I have written a C++ demo example. It transfers a file from a server to client.When, I run this program in the local host, it works fine. However, when I run this program over the network the file transfer is incorrect. The image size received is larger than the image size sent. Also, why the same works on the local host? I have tried changing the port number also.
Here is the program -
Server
/** man 2 socket **/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char msg[] = "Shreyas..first socket prog";
int sock , sock_active;
struct sockaddr_in server, client;
int sent,ret;
unsigned int len;
char buffer[1024];
FILE *fp;
if ( (sock = socket(AF_INET,SOCK_STREAM,0)) == -1 )
//if ( (sock = socket(AF_INET,SOCK_DGRAM ,0)) == -1 )
{
perror("Sock:");
}
server.sin_family = AF_INET;
server.sin_port = htons(15000);
//server.sin_addr.s_addr = INADDR_ANY;
inet_aton("136.170.195.17", &(server.sin_addr));
bzero(&server.sin_zero, 8);
len = sizeof(struct sockaddr_in);
if( ret = (bind( sock, (struct sockaddr *)&server, len)) == -1 )
{
perror("bind :");
}
ret = listen(sock, 0);
while(1)
{
if( (sock_active = accept(sock, (struct sockaddr *)&client, &len)) == -1 )
{
perror("Problem in active socket:");
}
fp = fopen("./Tiger.JPG","rb");
if( fp == NULL )
{
cout<<"Error open file";
return -1;
}
memset(buffer, 1024,0);
int packets = 0;
int count;
//while ( fgets(buffer,1024,fp ) != NULL )
while( ! feof(fp) )
{
packets++;
cout<<"Client IP address is "<<inet_ntoa(client.sin_addr)<<endl;
cout<<"Client Port address is "<<ntohs(client.sin_port)<<endl;
/** Fread is reliable when using to find out the EOF , in feof(fp **/
count = fread(buffer,1,sizeof(buffer),fp);
/** fgets doesn't move the FP correctly */
//fgets(buffer,sizeof(buffer),fp);
cout<<"Read number of bytes ="<<count;
sent = send(sock_active, buffer, sizeof(buffer),0);
cout<<"The number of bytes sent ="<<sent<<"packet number = "<<packets<<endl;
memset(buffer, 1024,0);
}
cout<<"CLose current socket"<<endl;
close(sock_active);
fclose(fp);
}
cout<<"CLosing socket now" <<endl;
close(sock);
return 0;
}
Client socket program -
/** man 2 socket **/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
struct sockaddr_in server;
int ret;
int sock;
int read_val = 1;
unsigned int len;
if ( (sock = socket(AF_INET,SOCK_STREAM,0)) == -1 )
{
perror("Sock:");
}
if( argc != 2 )
{
cout<<"Pass the Server IP "<<endl;
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_port = htons(15000);
inet_aton(argv[1], &(server.sin_addr));
bzero(&server.sin_zero, 8);
len = sizeof(server);
if ( (ret = connect(sock, (struct sockaddr *)&server,sizeof(server))) == -1)
{
perror("Connect failed:");
exit(-1);
}
char msg[1024];
memset(msg,0,1024);
FILE *fp_w;
fp_w = fopen("./try.JPG","wb");
while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
fwrite(msg,1,sizeof(msg),fp_w);
}
cout<<"Read is complete"<<endl;
fclose(fp_w);
close(sock);
return 0;
}
May be you should modify this block of code in your client.
while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
fwrite(msg,1,sizeof(msg),fp_w);
}
recv function will not always receive the number of bytes you want to receive. You have to use the return value of recv to know the number of bytes actually read and use that count to write to file.
I would write this block as,
while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
if ( read_val > 0)
{
fwrite(msg,1,read_val,fp_w); // I am using `read_val` while writing.
}
}
While working with file/socket io APIs, its not good to assume that it read/wrote exact number of bytes you asked to.
You are making the usual mistake of assuming that recv(), fread(), etc. fill the buffer. They aren't required to do that. They return a count of the number of bytes that were actually received. You have to use that count as the length argument when sending,
You have to send actual number of bytes read:
count = fread(buffer,1,sizeof(buffer),fp);
cout<<"Read number of bytes ="<<count;
if ( count > 0)
sent = send(sock_active, buffer, count,0);
The same for the client while reading:
while( read_val)
{
read_val = recv(sock,(char *)msg,sizeof(msg),0);
if ( read_val > 0) {
fwrite(msg,1,read_val,fp_w)
} else if ( red_val == -1) {
// an error has occured
break;
} else {
// peer has closed the connection
break;
{
}

how to send files in chunk using socket c/C++?

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...