Reputation: 329
I'm new to C programming and I've been trying to write a program to send a message to itself, receive the message, and send one back. I found an example of a client and server program and basically just copied it, trying to make a few changes. The program not only crashes, but outputs "ur message." Nowhere in the code is the string "ur message," so how is it possible that it outputs this every time?
I apologize for pasting a large amount of code, I just do not know where the problem is and I don't want to leave anything that might be relevant out. Any help would be greatly appreciated, thanks.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <pthread.h>
char *write_buffer_to_port(char *host, int portno);
char *listen_on_port(int portno);
void* listen_thread(void *arg);
char *buffer_a;
char *buffer_b;
int main(int argc, char *argv[]) {
buffer_a = "test";
buffer_b = "";
pthread_t tid[2];
pthread_create(&(tid[1]), NULL, &listen_thread, NULL);
printf("%s",write_buffer_to_port("localhost", 5737));
}
char *write_buffer_to_port(char *host, int portno) {
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) exit(1);
server = gethostbyname(host);
if (server == NULL) exit(2);
bzero((char *) &serv_addr, sizeof(serv_addr));
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);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) exit(3);
n = write(sockfd, buffer_a, strlen(buffer_a));
if (n < 0) exit(4);
n = read(sockfd, &buffer_a, 255);
if (n < 0) exit(5);
close(sockfd);
return (char *)buffer_a;
}
char *listen_on_port(int portno) {
int sockfd, newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) exit(1);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) exit(2);
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) exit(3);
//bzero(buffer_a,256);
n = read(newsockfd,&buffer_b,255);
if (n < 0) exit(4);
//printf("Here is the message: %s\n",(char *)&buffer_b);
n = write(newsockfd,"I got your message",18);
if (n < 0) exit(5);
return (char *)&buffer_b;
}
void* listen_thread(void *arg) {
printf("rcvd: %s\n",listen_on_port(5737));
return NULL;
}
Upvotes: 0
Views: 40
Reputation: 6739
"ur message" is the end of "I got your message".
You aren't using buffer_a
and buffer_b
correctly. The way you have them declared, they are pointers to char
. When you execute read(fd, &buffer_b, 255)
, you're reading into the memory occupied by the pointer buffer_b
, not into some buffer.
If you want to read into a buffer, you need to either declare the buffer as an object that has some space for you to read into, or declare it as a pointer and call malloc
to allocate memory for it. If you declare the buffers as char buffer_a[256]
, you will be able to read into them. That declaration reserves 256 bytes, and buffer_a
is the address of the beginning of that memory. You could then do read(fd, buffer_a, sizeof(buffer_a))
to read into the buffer.
I see that you're initializing buffer_a
to a string that you're going to write, which you could still do if you were declaring it as an array, but it would be simpler to just write the string itself.
Upvotes: 2
Reputation: 655
You're using unallocated buffers, char *buffer_a; char *buffer_b; So to fix it either allocate (malloc) it at main or declare them as arrays.
Upvotes: 0