In C I have created a client program and a server program, when the client sends the message it doesn't have any errors and sends it. On the other end the server receives it and says "message received printing on next line:" nothing is outputed here is the code
Client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main() {
char buffer[20] = "Hello from a client";
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Not Working\n");
return -1;
}
struct sockaddr_in feetpics;
feetpics.sin_family = AF_INET;
feetpics.sin_port = htons(8080);
feetpics.sin_addr.s_addr = inet_addr("127.0.0.1");
struct sockaddr *addr = (struct sockaddr *)&feetpics;
socklen_t sockbuf;
sockbuf = sizeof(feetpics);
if (connect(sockfd, addr, sockbuf) < 0 ) {
printf("couldn't establish a connection with 127.0.0.1\n");
return -1;
} else {
struct sockaddr_in jaycar;
struct sockaddr *radioshack = (struct sockaddr *)&jaycar;
socklen_t jaycarbuf = sizeof(jaycar);
getpeername(sockfd, radioshack, &jaycarbuf);
printf("connected to %s:%d\n", inet_ntoa(jaycar.sin_addr), ntohs(jaycar.sin_port));
size_t ben = strlen(buffer);
int flags = MSG_CONFIRM;
size_t result = send(sockfd, "Hello", 5, flags);
if (result < 0) {
printf("Message not sent\n");
return -1;
} else {
printf("Hello message sent\n");
}
}
}
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// By RCS
int main(int argc, char* argv[]) {
char buffer[100];
int port = atoi(argv[1]);
int incomingsocks;
int backlog = 3;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Failed\n");
return -1;
}
struct sockaddr_in socks;
socks.sin_family = AF_INET; // IPv4 address family
socks.sin_port = htons(port); // port number
socks.sin_addr.s_addr = inet_addr("127.0.0.1"); // IPv4 address
struct sockaddr *addr = (struct sockaddr *)&socks;
if (bind(sockfd, addr, sizeof(socks)) < 0) {
printf("FATAL: Socket could not be created\n");
return -1;
} else {
printf("NOTICE: Socket binded successfully \n");
if (listen(sockfd, backlog) < 0) {
printf("FATAL: Cannot Listen\n");
return -1;
} else {
printf("NOTICE: listening to port %d\n", ntohs(socks.sin_port));
}
socklen_t sockbuf;
sockbuf= sizeof(socks);
while (1) {
incomingsocks = accept(sockfd, addr, &sockbuf);
if (incomingsocks < 0) {
printf("FATAL: Won't accept connections\n");
return -1;
} else {
printf("NOTICE: Someone has connected\n");
}
int flags = MSG_WAITALL;
size_t receiveres = recv(incomingsocks, buffer, strlen(buffer), flags);
if (receiveres < 0) {
printf("NOTICE: No data sent from connected person\n");
}
if (buffer == NULL) {
printf("WARNING: A message has been received, but it wasn't written to the buffer");
} else {
printf("Message received printing on next line: \n");
buffer[receiveres] = '\0';
printf("%s", buffer);
}
}
}
}
I have tried to find alternatives for send and recv I have tried flags, and different ways opf outputting
size_t result = send(sockfd, "Hello", 5, flags);
if (result < 0) { // can never be true since size_t is unsigned.
Use ssize_t
as specificed as the return type for send
and recv
instead. You have the same issue on the receiving side.
Also, if(buffer == NULL)
doesn't make sense. buffer
is a char[100]
and will never be NULL
.
The problem making it impossible to receive messages is however that you tell recv
that the buffer size is strlen(buffer)
. That strlen
can return anything since buffer
is uninitialized and I'm guessing it returns 0
for you. You should use sizeof buffer
instead:
ssize_t receiveres = recv(incomingsocks, buffer, sizeof buffer, flags);
if (receiveres == -1) {
perror("recv");
} else if (receiveres == 0) {
puts("client shutdown");
} else {
puts("Message received printing on next line:");
printf("%.*s\n", (int) receiveres, buffer);
}
If you want to null terminate buffer
like you do now, use sizeof buffer - 1
instead.