<says sarcastically>i appreciate your help salem! Keep up the good work lol
Well with the help of our awesome member Salem, i solved the problem. On the client side, i was using sizeof instead of strlen on the write function. Thanks Salem!!
Let me post both the working client and server so i have a back up this time lol
CLIENT:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int sockfd, portno, readfd, writefd, yes = 1;
char buffer[1026];
struct hostent *server;
struct sockaddr_in serv_addr, cli_addr;
if(argc < 3) fprintf(stderr, "Ussage: %s IP Address port #", argv[0]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
fprintf(stderr, "%s.", strerror(errno));
exit(1);
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
bzero((char *) &serv_addr, sizeof(serv_addr));
server = gethostbyname(argv[1]);
if(server == NULL)
{
fprintf(stderr, "No such host.");
printf("%s", strerror(errno));
exit(1);
}
portno = atoi(argv[2]);
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if(connect(sockfd, (const struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr, "%s. CONNECT()\n", strerror(errno));
exit(1);
}
else
{
fprintf(stdout, "Made a connection to %s\n", inet_ntoa(cli_addr.sin_addr));
}
for( ; ; ) {
int i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer));
fprintf(stdout, "Message: ");
fgets(buffer, sizeof(buffer)-1, stdin);
if((writefd = write(sockfd, buffer, strlen(buffer))) < 0)
{
fprintf(stderr, "%s. WRITE(c)\n", strerror(errno));
exit(1);
}
/*if(writefd == 0)
{
printf("Nothing was written.\n");
exit(1);
}*/
i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer)-1);
if((readfd = read(sockfd, buffer, sizeof(buffer))) < 0)
{
fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr));
printf("%s. READ(c)\n", strerror(errno));
exit(1);
}
//Test to see if the buffer is blank. Uncomment to test.
/*if(readfd == 0)
{
printf("Null buffer. READ()\n");
exit(1);
}*/
else
{
fprintf(stdout, "%s", buffer);
}
}
close(sockfd);
return 0;
}
SERVER:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, readfd, writefd;
char buffer[1024];
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
printf("%s. SOCKET()", strerror(errno));
exit(1);
}
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);
//Bind returns zero upon success, and -1 on error.
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if(bind < 0)
{
printf("%s. BIND()", strerror(errno));
exit(1);
}
listen(sockfd, 10);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd < 0)
{
printf("%s. ACCEPT()", strerror(errno));
exit(1);
}
for( ; ; ) {
int i = sizeof(buffer); if(i > 0) bzero(buffer, 1024);
if((readfd = read(newsockfd, buffer, sizeof(buffer)-1)) < 0)
{
fprintf(stderr, "%s. READ(s)\n", strerror(errno));
exit(1);
}
//Test to see if the buffer is blank. Uncomment to test.
/*if(readfd == 0)
{
printf("Null buffer. READ()\n");
exit(1);
}*/
else
{
fprintf(stdout, "%s\n", buffer);
//If you use getchar() here, it will not double prompt, but a carriage retur is required.
}
i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer));
fprintf(stdout, "Message: ");
fgets(buffer, sizeof(buffer)-1, stdin);
if((writefd = write(newsockfd, buffer, sizeof(buffer)-1)) < 0)
{
fprintf(stderr, "%s. WRITE(s)\n", strerror(errno));
exit(1);
}
/*if(writefd == 0)
{
printf("Nothing was written.\n");
exit(1);
}*/
}
close(newsockfd);
close(sockfd);
return 0;
}
I appreciate all the help lol
Feel free to respond with extreme hostility, because of my sarcastic post lol Even tho half of the members on here do the same because they have a huge ego<puffs chest out and mockingly says> I'm a software engineer thats too good to respond with a simple response lol Im better than everyone else.
Psht, yeah ok.