I am having difficulty changing the amount of data my server sends and getting it to send data. This code fails, but works when I change the size of buffer
to 16 characters.
This is the code from transferclient.c
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define BUFSIZE 218
int main(int argc, char **argv) {
unsigned short portno = 10823;
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[32] = {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(portno);
printf("Attemping to connect\n");
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
printf("Connected\n");
read(sock, buffer, 16);
return 0;
}
This is the code from transferserver.c
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define BUFSIZE 218
int main(int argc, char **argv) {
int portno = 10823; /* port to listen on */
int server_fd, new_socket;
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);
}
// Attaching the socket to the port
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(portno);
// Forcefully attaching socket to the port
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);
}
printf("Listening\n");
while ((new_socket = accept(server_fd, (struct sockaddr *) &address, (socklen_t*) &addrlen)) > 0)
{
printf("%d\n", new_socket);
printf("Socket connected\n");
}
return 0;
}
Server side
Listening
4
Socket connected
***Client Side***
```
Attemping to connect
Connected
When I change the line in transferclient.c to 32 bytes.
char buffer[32] = {0};
The sockets fail to connect.
Server side
Listening
Client Side
Attemping to connect
Why is that?