I have the following structure in my header file structure.h. Now i need to use this structure in my main.c file.
I need to fill this structure with some values and I need to send this from TCP/IP client to the TCP/IP server on the same system.
#ifndef STRUCTURE_H
#define STRUCTURE_H
typedef struct
{
unsigned int variable3;
char variable4[8];
}NUMBER_ONE,*PNUMBER_ONE;
typedef struct
{
unsigned int variable5;
char variable6[8];
}NUMBER_TWO,*PNUMBER_TWO;
typedef struct
{
char name[32];
unsigned int a;
unsigned int b;
NUMBER_ONE variable1;
NUMBER_TWO variable2;
}NUMBER_THREE,*PNUMBER_THREE;
#endif
I have tried this but, I am not good in C, so please can anyone tell me how to do it, by taking the above structure as an example? Till socket connection establishment is ok for me,
but after establishing connection, how do I send this structure from the client to the server?
I am doing this in my Linux Ubuntu 12.04 system.
When sending information using sockets three ways used:
1)fixed size message (we will use it, plus assuming we are writing on the same machine byteorder match).Simply, like we will send 100 bytes and on receive we will read 100bytes
2)message.len + message.( first we send message len then message itself. used for binary send receive mostly)
3)marker method (mostly used sending text messages or commands. for exampling marking with \n newline)
Next coming on representing our data (serialize). It is easy with c cause on c we can directly write our object and retrieve it without additional efforts.Object will be the same as in memory.
// PNUMBER_THREE structAddr;
send(socket_id, structAddr, sizeof(NUMBER_THREE), 0);
or
write(socket_id, structAddr, sizeof(NUMBER_THREE));
or safer
write_socket(socket_id, structAddr, sizeof(NUMBER_THREE));
//It is safer to do so though we are using blocking mode
int write_socket(int fd,const char *buf,int len){
int currentsize=0;
while(currentsize<len){
int count=write(fd,buf+currentsize,len-currentsize);
if(count<0) return -1;
currentsize+=count;
}
return currentsize;
}
when reading we will be using the same structure plus it must meet condition sizeof(NUMBER_THREE)==SizeInsideClient //SizeInsideClient is sizeof on client SizeInsideClient=sizeof(NUMBER_THREE)
//SizeInsideClient structure size on client program
assert(sizeof(NUMBER_THREE)==SizeInsideClient);
readblock(socket_id,structAddr,sizeof(NUMBER_THREE));
int readblock(int fd, char* buffer, int len) {
int ret = 0;
int count = 0;
while (count < len) {
ret = read(fd, buffer + count, len - count);
if (ret <= 0) {
return (-1);
}
count += ret;
}
return count;
}
Short example with no error checking:
Server
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
Client
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(5000);
inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
connect(sockfd,
(struct sockaddr *)&serv_addr, sizeof(serv_addr))
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
Pitfalls
Is not "safe" so send plain struct. Sooner or later you will deal with endianness (byte order), packing (which can still be a problem even with #pragma pack) and sizes of types like 'int' that can vary between platforms
Related
So I'm doing some benchmarking of TCP to study the impact of the amount of data transferred per connection over the resulting bandwidth. So I wrote a server and a client in C to measure what I need. I then used a Python script to run the experiments many times (To have a precision of +/- 1% I ran my test for 100s. For each point of data I ran the experiment 33 times to get a decent average.), with different inputs, and to gather the results.
The results I get seem about right (I can even observe the expected plateau at higher amounts of data transferred per connection) with the exception that the bandwidth is only 10% of what it should be...
Because I only have access to one computer to run this benchmark, I'm doing the tests on localhost, but that shouldn't be an issue.
Here are my results:
As you can see, it seems the best bandwidth I can get is a bit more than 300 MB/s... But if I run a bandwidth test with iperf (I made sure to use the same TCP window size), on localhost I get a bandwidth of about 3 GB/s.
Here is the code of my client:
int main(int argc, char const *argv[])
{
unsigned int size;
unsigned int timeout;
int sockfd;
struct sockaddr_in server_addr;
if(argc < 4 || argc > 5 ){
usage();
exit(1);
}
const char * ip = "127.0.0.1";
ip = argv[3];
int port = PORT;
if(argc == 5) {
port = atoi(argv[4]);
}
size = atoi(argv[1]);
timeout = atoi(argv[2]);
unsigned int count = 0;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
while((end.tv_sec - start.tv_sec) < timeout) {
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Could not create socket\n");
exit(0);
}
bzero(&server_addr, sizeof(server_addr));
// assign IP, PORT of the server
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) {
perror("connection with the server failed");
exit(1);
}
unsigned int nread = 0;
unsigned int nreadcum = 0;
char* buf = malloc(size);
char* bufbuf = buf;
while(nreadcum < size){
nread = read(sockfd, bufbuf, size-nreadcum);
nreadcum += nread;
bufbuf+=nread;
}
// close connection
close(sockfd);
count++;
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
free(buf);
}
uint64_t sec = (end.tv_sec - start.tv_sec);
double bandwidth = (count*size)/sec;
printf("%u,%lf,%u,%lu\n", size, bandwidth, count, sec);
return 0;
}
And here is the code of my server:
int serv_sock_fd;
int main(int argc, char const *argv[])
{
int size;
struct sockaddr_in serv_addr;
struct sockaddr_in client_addr;
int bound_port;
if(argc != 2){
usage();
exit(1);
}
size = atoi(argv[1]);
int serv_sock_fd = socket(AF_INET,SOCK_STREAM,0);
int true = 1;
setsockopt(serv_sock_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int));
if(serv_sock_fd == -1) {
perror("Failed to open server socket");
exit(1);
}
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
// Bind socket to the chosen port
if ((bound_port = bind(serv_sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) <0){
perror("Could not bind socket to local port");
exit(1);
}
// Listen on the port
if (listen(serv_sock_fd, 16))
{
perror("Could not listen");
exit(1);
}
signal(SIGINT, sigint_handler);
printf("Waiting for connection on %d ...\n", PORT);
int returned = 1;
while(returned) {
printf(".\n");
int new_socket_fd;
unsigned int client_addr_len = sizeof(client_addr);
if ((new_socket_fd = accept(serv_sock_fd, (struct sockaddr *)&client_addr,
&client_addr_len))<0) {
perror("Could not accept client connection");
exit(1);
}
printf("connection received, start sending ... ");
char * payload = sequence_payload(size);
returned = write(new_socket_fd, payload, size);
printf("finished sending\n");
printf("Returned value = %d\n", returned);
close(new_socket_fd);
free(payload);
}
close(serv_sock_fd);
return 0;
}
char * sequence_payload(int size) {
char * payload = malloc(size);
for (int i = 0; i < size; i++)
{
payload[i] = i%256;
}
return payload;
}
Basically what my code is doing is:
for the server: wait for a client to connect, send a dummy payload of the size wanted and close the connection to that client, repeat.
for the client: open a connection to the server, read what the server is sending until the server closes the connection, repeat until the decided timeout is reached.
To calculate the bandwidth, I can just do (number_of_connections_completed * size_transferred_by_connection) / duration_of_all_transfers. I use python to calculate the bandwidth, to be free of any overflow in C.
TLDR: The bandwidth I get with my C programs is 10 times less than what it should be on localhost. What could be the source of that problem?
malloc and free are the main issue here. Since they are system call they take a significant amount of time, and since I am measuring the performance of TCP and not those of memory allocation, malloc and free should be outside my profiled loop. Also, the same thing applies to printf in the server-side loop, while not as bad as malloc, the time it takes to print something on the screen is not something that should be taken into account when measuring the performance of TCP.
I want to send a zero copy in tcp. But I do not know how to send the data in the sendfile.
Please tell me how the tcp zero copy of the socket communication.
#define BUFSIZE 10240
#define NUMBER 2
int main(void)
{
struct sockaddr_in server;
int s, n;
char buf[BUFSIZE];
char ada[BUFSIZE];
int optval = 1;
int i=0;
int j;
memset((void *) ada, (int)'a', sizeof(ada));
s = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(s, SOL_TCP, TCP_CORK, &optval, sizeof(int));
memset((char *)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(5320);
connect(s, (struct sockaddr *)&server, sizeof(server));
for(j = 0; j <NUMBER ; j++){
memset((void *) ada,i+64, sizeof(ada));
sendto(s, ada, strlen(ada), 0, NULL, 0); // want to send zero-copy
i++;
sleep(1);
}
optval = 0;
setsockopt(s, SOL_TCP, TCP_CORK, &optval, sizeof(int));
close(s);
exit(0);
}
Also, is there a zero-copy send in other ways?
Zero-copy means no data copying between user space and kernel space. In this program, we are copying data from the user space buffer to kernel space to transmit that data using socket.
When we need to transfer data out from a file using a socket, we can use sendfile system call, passing it the socket and file descriptor.
Please refer to the link http://www.linuxjournal.com/article/6345?page=0,0 which gives useful information on zero copy.
I have two server codes:
the first server: send the client a char each time until the string is finished
int
main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
char temp[1];
int i = 0;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(9999); /* daytime server */
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
for ( ; ; ) {
connfd = Accept(listenfd, (SA *) NULL, NULL);
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
for(i = 0; i < strlen(buff); i++)
{
temp[0] = buff[i];
Write(connfd, temp, strlen(temp));
}
Close(connfd);
}
}
the second server: send the client a string
int
main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
char temp[1];
int i = 0;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(9999); /* daytime server */
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
for ( ; ; ) {
connfd = Accept(listenfd, (SA *) NULL, NULL);
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(connfd, buff, strlen(buff));
Close(connfd);
}
}
the client:receive the chars sent by the server
int
main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
int count = 0;
if (argc != 2)
err_quit("usage: a.out <IPaddress>");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(9999); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");
while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = 0; /* null terminate */
count++;
if (fputs(recvline, stdout) == EOF)
err_sys("fputs error");
}
if (n < 0)
err_sys("read error");
printf("read time:%d\n", count);
exit(0);
}
the result is both of the output of variable count is 1. My question is why the first server's output is 1, I think the result should be strlen(buff) for the 1st server?
PS:I run the server and client on the same machine.
TCP is a stream protocol. As such the number of writes on one side will not cause the same amount of reads on the other side since the protocol doesn't preserve information about how the writes into the socket were made.
Usually, on the sender side there's a delay before a packet is sent in case you write more data to the socket so that more data can be stuffed into the same packet. One of the reasons for it is that a badly written server might flood the network with single byte packets.
On the receiver side, the protocol doesn't know why your data might have arrived as separate packets, it might have been split up because of the MTU, it might have been reassembled by some packet inspection software or appliance on the way, so whenever you read from your socket it will give you as much data as it can regardless of how it was sent to you.
On a local machine like in your setup it's likely that the client isn't even running while the server is writing, so even without buffering on the sender side it will not start reading until the server has written everything and therefore it will read everything in one go. Or not, you might be unlucky, your server gets preempted for long enough that the TCP implementation in your kernel thinks that there won't be any more data you'll be sending, send a single byte to the client, the client gets scheduled to run before the server runs again and the client will receive just one byte in the first read.
I am new in Unix/Linux networking programming, so I have written server-client program in below.In this code there is one socket between client and server, client requests to server, then server responses from 1 to 100 numbers to client. So my question is how can we do this process with 3 socket( tcp connection) without using thread? ( e.g. First socket runs then second runs then third runs then first again. ) Do you have any suggestion?
Client.c
int main()
{
int sock;
struct sockaddr_in sa;
int ret;
char buf[1024];
int x;
sock = socket (AF_INET, SOCK_STREAM, 0);
bzero (&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(SERVER_PORT);
inet_pton (AF_INET, SERVER_IP, &sa.sin_addr);
ret = connect (sock,
(const struct sockaddr *) &sa,sizeof (sa));
if (ret != 0) {
printf ("connect failed\n");
exit (0);
}
x = 0;
while (x != -1) {
read (sock, buf , sizeof(int));
x = ntohl(*((int *)buf));
if (x != -1)
printf ("int rcvd = %d\n", x);
}
close (sock);
exit (0);
}
Server.c
int main()
{
int list_sock;
int conn_sock;
struct sockaddr_in sa, ca;
socklen_t ca_len;
char buf[1024];
int i;
char ipaddrstr[IPSTRLEN];
list_sock = socket (AF_INET, SOCK_STREAM, 0);
bzero (&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(SERVER_PORT);
bind (list_sock,(struct sockaddr *) &sa,sizeof(sa));
listen (list_sock, 5);
while (1){
bzero (&ca, sizeof(ca));
ca_len = sizeof(ca); // important to initialize
conn_sock = accept (list_sock,(struct sockaddr *) &ca,&ca_len);
printf ("connection from: ip=%s port=%d \n",inet_ntop(AF_INET, &(ca.sin_addr),
ipaddrstr, IPSTRLEN),ntohs(ca.sin_port));
for (i=0; i<100; ++i){
*((int *)buf) = htonl(i+20);
// we using converting to network byte order
write (conn_sock, buf, sizeof(int));
}
* ((int *)buf) = htonl(-1);
write (conn_sock, buf, sizeof(int));
close (conn_sock);
printf ("server closed connection to client\n");
}
}
I think it best to look at the excellent resource Beej's Guide to Netwokr Programming which goes into detail about this. He also has some good examples which you can use as a starting point and he covers all the major platforms including windows.
Basically you do:
socket()
bind()
listen()
accept()
accept() returns a socket connected to a unique client. Then you'd use select, poll or epoll to determine when data is available on those sockets. I suggest you can look at the man pages for these API's and Beej's guide. It's where I first learned network programming.
Looking at your code, your inner loop is wrong. When you accept a connection, you need to add it to a list or something. Currently, you overwrite it and loose it. You should use (e)poll or select to tell you which has data. You can write to any of them at any time. Again, look at the examples in Beej's guide, they are most helpful.
Maybe it's not exactly what you want,but I think you could try epoll,There is a simple example
typedef struct event_loop
{
int max_event;
int epfd;
}event_loop;
event_loop* create_event_loop()
{
event_loop *ep = malloc(sizeof(event_loop));
ep->max_event = 512;
ep->epfd = epoll_create(512);
return ep;
}
int add_event(event_loop *ep, int fd)
{
epoll_event ee;
ee.data.fd = fd;
ee.event = EPOLLIN | EPOLLPRI;
epoll_ctl(ep->epfd, EPOLL_CTL_ADD, fd, &ee);
}
void event_main(event_loop *ep, int listenfd)
{
epoll_event events[512];
int nfds, i, newfd;
while(1)
{
if(nfds = epoll_wait(ep->epfd, events, 512, -1) == -1)
exit(1);
for(i = 0; i < nfds; i++)
{
if(events[nfds].data.fd == listenfd)
{
newfd = accept(listenfd, NULL, NULL);
add_event(ep, newfd);
}
else
{
//do what you want
}
}
}
}
epoll is a high-efficiency solution,just man epoll get more information
I had earlier posted a question, regarding same, but over here i want guidance for my code. Using the tips from people I have tried to create for sending a packet. My max packet structure alongwith header and payload is of 16 bytes.Kindly if possible glance through the sending and receiving code and suggest where i am going wrong. Basically my client keeps sending data to server,it just doesn't end and server doesn't show results.
Client:
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
struct packet
{
long int srcID;
long int destID;
long int pver;
long int profiles;
char length;
long int data;
};
if (argc < 3) {
fprintf(stderr,"usage: %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]); //Convert ASCII to integer
sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor
if (sockfd < 0)
error("ERROR DETECTED !!! Problem in opening socket\n");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server address
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR in connection");
printf("SUCCESS !!! Connection established \n");
char buffer[128];
struct packet *pkt = (struct packet *) buffer;
char *payload = buffer + sizeof(struct packet);
long int packet_size;
printf("Started Creating packet\n");
pkt->srcID = 0x01;
pkt->destID = 0x02;
pkt->pver = 0x01;
pkt->profiles = 0x01;
pkt->length = 128;
pkt->data = 1; 2; 3; 4; 5; 6; 7; 8;
if (send(sockfd,pkt,sizeof(packet_size),0) <0)
printf ("error\n");
else
printf ("packet send done");
return 0;
}
Server:
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
char wish;
long int SrcID;
long int DestID;
long int Pver;
long int Profiles;
long int Data;
char Length;
char bytes_to_receive;
char received_bytes;
struct packet
{
long int srcID;
long int destID;
long int pver;
long int profiles;
char length;
long int data;
};
if (argc < 2) {
fprintf(stderr,"usage: %s port_number1",argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR DETECTED !!! Problem in opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR DETECTED !!! There was a problem in binding");
listen(sockfd, 10);
clilen = sizeof(cli_addr);
printf("Server listening on port number %d...\n", serv_addr.sin_port);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR DETECTED !!! the connection request was not accepted");
char buffer[128];
struct packet *pkt = (struct packet *) buffer;
char *payload = buffer + sizeof(struct packet);
long int packet_size;
bytes_to_receive = sizeof(pkt);
received_bytes = 0;
if (recv(newsockfd, pkt, sizeof(pkt), 0) < 0)
error("ERROR DETECTED !!! There was a problem in reading the data");
else
{
do {
received_bytes += (buffer + received_bytes, bytes_to_receive - received_bytes);
} while (received_bytes != bytes_to_receive);
SrcID = pkt->srcID;
DestID = pkt->destID;
Pver = pkt->pver ;
Profiles = pkt->profiles;
Length = pkt->length;
Data = pkt->data;
printf("Data Received from Client_1 are :\n");
printf("Source ID: %l\n", SrcID);
printf("Destination ID: %l\n", DestID);
printf("profile Version: %l\n", Pver);
printf("No of Profiles: %l\n", Profiles);
printf("Length: %l\n", Length);
printf("data : %l\n", Data);
}
if (close(newsockfd) == -1) {
error("Error closing connection with client 1");
}
printf("Connection with client 1 has been closed\n");
return 0;
}
The server is not showing any o/p. Client says it has send the packet. While compiling the server code i see four warnings saying unknown conversion type characters 0xa in format for all the printf statements in server code. I guess I am going wrong somewhere in the server code side, but I am not able to follow the "serialization". Please update me with your inputs, it would be of great help.
Here is couple of issues that I found:
Your client keep sending packages because it is in infinite while
loop.
You passed wrong len parameter of recv function. Right now
you pass sizeof(packet_size) which is equal to sizeof(long int) (4
bytes on 32 bit OS), but probably your intension was to use
sizeof(packet) (16 bytes).
You don't check how many bytes were
truly read by recv function. With TCP you don't have guaranties that
you read all 16 bytes of struct packet. So from time to time you
could read less bytes and your packet will be incomplete. Here is an
example in some pseudo code how you should receive whole packet:
bytes_to_receive = sizeof(packet)
received_bytes = 0;
do {
received_bytes += recv(buffer + received_bytes, bytes_to_receive - received_bytes)
} while (received_bytes != bytes_to_receive)
Your struct packet in client and server is different. In one you use char length; in second long int length;
I think also this kind of assignments in server make no sense pkt->srcID = SrcID; and should be something like this SrcID = pkt->srcID;
The problem with the client continually sending is because you simply have it in a loop. With indentation fixed, it becomes clear what has happened:
while (1)
{
if (send(sockfd,pkt,sizeof(packet_size),0) <0)
printf ("error\n");
else
printf ("packet send done");
}
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);