I'm either very tired and not noticing something simple , or this is completely screwing with me. I'm getting a segmentation fault ( core dumped ) and I've managed to pinpoint it to the sendto() in the worker function. (in the server)
Server code:
//UDPServer.c
/*
* gcc -o server UDPServer.c
* ./server <port> <buffersize>
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void err(char *str)
{
perror(str);
exit(1);
}
int sock;
typedef struct
{
struct sockaddr_in client;
int buffsize;
char *msg;
} data;
void *worker (void* asd)
{
int len;
FILE *fp;
data d;
d = *(data*) asd;
char buff[d.buffsize];
printf("Received packet from %s:%d\nData:%sSize:%d\n",
inet_ntoa(d.client.sin_addr), ntohs(d.client.sin_port)
,d.msg,d.buffsize);
char * fn;
memcpy (fn,d.msg,strlen(d.msg)-1);
fp = fopen(fn,"rb");
int bytes;
len = sizeof(d.client);
printf ("%d\n",len);
while (bytes=fread(buff,sizeof(char),d.buffsize,fp))
{
printf ("Server sent %d bytes.\n",bytes);
-> this if right here. this causes the core dump when attempting to send
if(sendto(sock , &buff , sizeof(buff),0,(struct sockaddr *)&d.client,len)<0)
err("Error sending.");
}
fclose(fp);
}
int main(int argc, char** argv)
{
struct sockaddr_in server, client;
int port, i;
socklen_t slen=sizeof(client);
if(argc != 3)
{
printf("Usage: <Port> <Bytes>\n");
exit(0);
}
else
sscanf(argv[1],"%d",&port);
int buffsize = atoi(argv[2]);
char buff[buffsize];
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
err("socket");
else
printf("Server : Socket() successful\n");
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr* ) &server, sizeof(server))==-1)
err("bind");
else
printf("Server : bind() successful\n");
while(1)
{
memset(&buff,0,sizeof(buff));
if (recvfrom(sock, &buff, sizeof(buff), 0, (struct sockaddr*)&client, &slen)==-1)
err("recvfrom()");
data d;
d.client = client;
d.buffsize = buffsize;
d.msg = buff;
pthread_t t;
pthread_create(&t,NULL,worker,&d);
pthread_join(t,NULL);
}
return 0;
}
I don't think the client is relevant here since it's only job is to send the filename. The read works btw , I've tested.
Anyway , I'm just trying to send the content of the file for the moment.I've been trying to figure this out for the past hour and for the life of me I can't find out what's it's problem. The segmentation fault makes no sense to me.
Any suggestions are greatly appreciated.
I'd be nervous about the sizeof(buff) in the sendto. buff's size is fixed at runtime based on the argument. But sizeof is a compile-time operation. (Or at least it was back in the good old days - I'm not sure about C99) Oh, nevermind - I see that has changed
Still, why not use d.buffsize there instead? Or maybe bytes, since you might not have filled the buffer.
Although #21Zoo is wrong about dynamic arrays in C99, I think he found the root problem
char * fn;
memcpy (fn,d.msg,strlen(d.msg)-1);
fn has no memory allocated to copy into, so you are writing to a random point in memory.
Something in the sendto is probably stumbling over that memory which now contains garbage.
You either need to malloc(strlen(d.msg)+1) or use strdup instead.
Related
I'm working on a multithreaded server/client. The problem I have is that the server handling sometimes looks a little bit various. The message, which is send back is always correct, but the message the server prints out is a little bit weird. If it is a short word like "hello" everything works. If it is a long word or there are spaces in the string like "Binominalkoeffizient" the out-printed serversided message is:
Binomina
lkoeffiz
ient
fiz
Any idea where my mistake is?
PS: The server reaction is the same when I use telnet!
Server-Main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include "server.h"
int main(int argc, const char * argv[]) {
int sock;
struct sockaddr_in server;
sock = socket(AF_INET, SOCK_STREAM, 0);
socketStatusCheck(sock);
puts("[*] Starting Server ...");
puts("[*] Initialize Server ...");
initializeServer(&server, 8888);
bindServerToAddress(sock, server);
puts("[*] Waiting for incomming connections ... ");
puts("");
listen(sock, 3);
connectionSwitch(sock);
close(sock);
return 0;
}
Server-File
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include "server.h"
void socketStatusCheck(int sock) {
if (sock == -1) {
perror("Error creating the socket: ");
exit(0);
}
}
void initializeServer(struct sockaddr_in *server, int port) {
server->sin_family = AF_INET;
server->sin_addr.s_addr = INADDR_ANY;
server->sin_port = htons(port);
}
void bindServerToAddress(int sock, struct sockaddr_in server) {
if (bind(sock, (struct sockaddr*) &server, sizeof(server)) < 0) {
perror("Error binding port: ");
}
}
void connectionSwitch(int sock) {
int nsock, lenbuf;
struct sockaddr_in client;
pthread_t pid = NULL;
lenbuf = sizeof(struct sockaddr_in);
while ((nsock = accept(sock, (struct sockaddr*) &client, (socklen_t*) &lenbuf))) {
puts("Client connected!");
if (pthread_create(&pid, NULL, connectionHandler, (void*) &nsock))
perror("Error creating thread: ");
}
if (nsock < 0) {
perror("Error accepting incomming client: ");
}
pthread_exit(pid);
}
void *connectionHandler(void *sockptr) {
int sock = *(int*) sockptr;
long isConnected;
char *smessage, *recvmessage;
smessage = "Hello! I am the server you just connected! \n";
write(sock, smessage, strlen(smessage));
recvmessage = malloc(5000 * sizeof(char)); // while ((isConnected = recv(sock, recvmessage, sizeof(recvmessage), 0)) > 0)
while ((isConnected = recv(sock, recvmessage, sizeof(recvmessage), 0)) > 0) {
//write(sock, recvmessage, sizeof(recvmessage));
send(sock, recvmessage, sizeof(recvmessage), 0);
puts(recvmessage);
}
if (isConnected == 0) {
perror("Client disconnected: ");
fflush(stdout);
}
free(recvmessage); recvmessage = NULL;
return 0;
}
This really has nothing to do with multithreading, and everything to do with the nature of SOCK_STREAM sockets.
Stream sockets are, as the name suggests, a stream of bytes; they do not preserve message boundaries such that what is sent with one call to send is received with one call to recv. A single send may be broken up across multiple recv calls, or multiple send calls may be coalesced into a single recv, or both. They do guarantee order, in that the bytes will be received in the same order they are sent.
You'll need to implement your own record marking, perhaps by inserting \0 characters to delimit words, or by using length prefixes.
This is normal behavior. When you use send you don't know how many bytes will be sent. It may happen that all the words , characters are sent.However there are ways to solve this problem. One way is to write a simple header to the string you send , which contains the length of the string you are sending . So you know when the string is ending . For example you can use a thread to look continuously for messages and because the header contains the length of the string you know when to print a \n.The behavior of the send cannot be altered , because it is the Kernel , that is doing this .
In addition to what the other answers say already:
Your code specifically asks to read 8 bytes at a time. recvmessage is a pointer, and pointers are 8 bytes on your system, so sizeof(recvmessage) is 8.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a client / server system in which the client sends a record from client to server. I am trying to represent the data as a struct, but I do not successfully recv() the struct at the server end.
Here is my code:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#pragma pack(1)
struct emp
{
int id;
char *name;
} emp1, emp3;
#pragma pack(0)
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
memset(recvBuff, '0', sizeof(recvBuff));
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
serv_addr.sin_addr = inet_addr("192.168.0.105");
if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
emp1 = (struct emp *) malloc(sizeof(struct emp));
emp1.name = (char *) malloc((size)*(sizeof(char)));
printf("Enter name of employee : ");
scanf(" %[^\n]s", emp1.name);
printf("Enter emp id : ");
scanf(" %d", &emp1.id);
unsigned char *data = (unsigned char*) malloc(sizeof(regn));
memcpy(data, ®n, sizeof(regn));
sendto(sockfd, data, sizeof(emp1), 0, (SOCKADDR *) &serv_addr,
sizeof(serv_addr));
close(sockfd);
}
and my server is like this
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
struct emp
{
int id;
char *name;
} emp1, emp3;
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
FILE *fp, *fp1;
char recvBuf[1025];
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
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);
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
if(recvfrom(sd, recvBuf, recvBufSize, 0,
(struct sockaddr *) &clientAddr, &len) < 0)
{
printf("Error receiving message from client\n");
}
else
{
printf("Message received:%s\n", recvBuf);
printf("Size :%d\n", strlen(recvBuf));
memcpy(&emp1, recvBuf, sizeof(emp));
printf("name:%s\n", emp1.name );
printf("is:%d\n", emp1.id);
}
you are sending a struct which contains an integer and a char pointer. This pointer points to a completly random location, on the receiving system.
You have to send the content name points to.
You could send every member of the struct on its own. For members with variable length, you have to send the size first and allocate it on the receiving end.
As stated by John Bollinger: One of your structs is packed, the other not. this may cause additional problems with layout and size of the struct and is probably unnecessary .
Added this answer to answer to specific questions of the OP (it may seem a bit off track of the original question).
An example of simple streaming. It is simple because in a real streaming scenario you need object id's to be able to instantiate and load the real classes. But if you know already what class you are sending or receiving, their is no need for this complexity. If you are sending multiple instances of classes then you need a more complex streaming mechanism. As microsoft did for instance in MFC, Borland in OWL, .NET also has mechanisms in place, ...
namespace OMCIPoco{
typedef std::vector<unsigned char> ucharvector;
SystemData::stream& operator <<(SystemData::stream& stream,const ucharvector& data);
SystemData::stream& operator >>(SystemData::stream& stream,ucharvector& data);
}
The cpp file :
SystemData::stream& operator <<(SystemData::stream& stream,const ucharvector& data)
{
stream << (unsigned long)data.size();
for (ucharvector::const_iterator it=data.begin();it<data.end();++it)
stream << *it;
return stream;
}
SystemData::stream& operator >>(SystemData::stream& stream,ucharvector& data)
{
unsigned long count;
stream >> count;
data.reserve(count);
for (unsigned int i=0;i<count;++i){
unsigned char c;
stream >> c;
data.push_back(c);
}
return stream;
}
Notice how the << and >> are in balance.
The stream is an interface that makes abstraction of the medium you are streaming to, it can be memory or disk or a networkstream. I can solve little endian problems using the streaming concept here as well, because the stream knows how to (de)serialise shorts, longs, ..., and it knows howto buffer as well.
You can use a flat structure as well avoiding the above complexity completely
class data{
public:
char name[20];
};
now using sizeof(data) will be correct because you will send the entire data struct. I used class here but struct works just the same way. The downside is that if the name contains only 10 charakters you will still send 20.
I am writing a simple program using sockets to receive actual date from server.
I am getting this error and i don't know where i am making a mistake.
Client:
/* Make the necessary includes and set up the variables. */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main ()
{
int sockfd;
socklen_t len;
struct sockaddr_in address;
int result;
int id=2, answer, length;
char *s;
/* Create a socket for the client. */
sockfd = socket (AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr ("127.0.0.1");
address.sin_port = htons (9734);
len = sizeof (address);
/* Now connect our socket to the server's socket. */
result = connect (sockfd, (struct sockaddr *) &address, len);
if (result == -1)
{
perror ("oops: netclient");
exit (1);
}
/* We can now read/write via sockfd. */
write(sockfd, &id, sizeof(id)); /* sending the request id */
read(sockfd, &answer, sizeof(answer)); /* receiving the answer id*/
if(answer==1002){
printf("Odebrano wlasciwa odpowiedz\n");
read(sockfd, &length, sizeof(length)); /* receiving the answer string length*/
s=(char*)malloc(length*sizeof(char)); /* receiving the string with the date */
read(sockfd, s, length);
printf ("Date from server = %s\n", s);
}
free(s);
close (sockfd);
exit (0);
}
Server:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
int main ()
{
int server_sockfd, client_sockfd;
int length;
char *s;
int id;
int answer=1002;
socklen_t server_len, client_len;
time_t rtime;
struct tm *timeinfo;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket (AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl (INADDR_ANY);
server_address.sin_port = htons (9734);
server_len = sizeof (server_address);
bind (server_sockfd, (struct sockaddr *) &server_address, server_len);
/* Create a connection queue and wait for clients. */
listen (server_sockfd, 5);
while (1)
{
printf ("server waiting\n");
/* Accept connection. */
client_len = sizeof (client_address);
client_sockfd = accept (server_sockfd,
(struct sockaddr *) &client_address,
&client_len);
/* We can now read/write to the client on client_sockfd.
The five second delay is just for this demonstration. */
read(client_sockfd, &id, sizeof(int)); /*receive request id */
if(id==2){
write(client_sockfd, &answer, sizeof(int)); /* sending an answer_id*/
time(&rtime);
timeinfo=localtime(&rtime);
s=(char*)malloc(sizeof(asctime(timeinfo))*sizeof(char));
printf("%s\n", asctime(timeinfo));
s=asctime(timeinfo);
printf("Size of s:%lx\n", sizeof(s));
length = htons(strlen(s));
write (client_sockfd, &length, sizeof(length)); /* sending the answer string length to the client */
printf("Date: %s\n", s);
write (client_sockfd, s, length); /* sending string with date to the server */
}
free(s);
close (client_sockfd);
}
}
I am almost certain that there is something wrong with allocing/freeing the space with the string containing actual date but i can't see the actual mistake.
EDIT:
Actually, i didn't know how to solve this problem in this mentioned way but i've came up with other idea.
I just send the result of asctime(timeinfo) over the server without using char* s.
So i don't write the date to char *s.
Program works fine now with no errors but i guess there is a way to do it some other way.
Well, even though, many thanks for the help, was helpful.
In your server, you overwrite s with asctime(...) after allocating it. This leaks the original memory. Also, the return value of asctime is a static buffer that cannot be freed.
Here's an excerpt from your code:
char *s;
if(answer==1002){
printf("Odebrano wlasciwa odpowiedz\n");
read(sockfd, &length, sizeof(length)); /* receiving the answer string length*/
s=(char*)malloc(length*sizeof(char)); /* receiving the string with the date */
printf ("Date from server = %s\n", s);
}
free(s);
Note what happens if 'answer' is not equal to 1002 -- you're calling free() on an uninitialized value. That is likely the cause of the error you are seeing. (This same mistake is present in both programs)
Refering the server code:
Here you allocate memory to s:
s=(char*)malloc(sizeof(asctime(timeinfo))*sizeof(char));
Here you overwrite the pointer with the value receive from asctime() so the original value returned by malloc() is lost introducing a memory leak:
s=asctime(timeinfo);
Here you then try to free what had been received from asctime(), which is a reference to static memory and gherefore cannot be freed.
free(s);
I'm just learning how to create servers in C and I keep getting a "segmentation fault: 11" error when i try telnetting into this server from another terminal. any help would be greatly appreciated!
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void error(char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}
int main(int argc, char *argv[])
{
char *advice[] = {
"Take smaller bites\r\n",
"Go for the tight jeans. No they do NOT make you look fat.\r\n",
"One word: inappropriate\r\n",
"Just for today, be honest. Tell your boss what you *really* think\r\n",
"You might want to rethink that haircut\r\n"
};
int listener_d = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in name;
name.sin_family = PF_INET;
name.sin_port = (in_port_t)htons(30000);
name.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listener_d, (struct sockaddr *) &name, sizeof(name))== -1)
error("Cannot bind");
if (listen(listener_d, 10) == -1)
error("Cannot listen");
puts("Waiting connection");
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d, (struct sockaddr *) &client_addr, &address_size);
char msg = advice[0];
if (send(connect_d, msg, strlen(msg), 0) == -1)
error("Cannot send");
close(connect_d);
}
return 0;
}
You're storing advice[0] into a char rather than a char *. It won't fit, and is truncated, making the pointer invalid. Store it in a char * (or even const char *) instead.
I'd also recommend turning on warnings in your compiler, since they would have caught this mistake. For GCC and Clang, I use -Wall -Wextra -pedantic.
I am new to network programming, and have been learning this by writing small programs that make use of the Socket API. Currently, I am writing a simple echo server, that uses fork to create a copy of it, as soon as it gets a connect request, this adds up as in improvement over the previous Iterative server (here). However, after I start the server and fire up the client, and type a message on its console, it quits unexpectedly. Running the program under Gdb shows that SIGPIPE was delivered. But as far as I know as the socket is still valid, a SIGPIPE shouldn't have occured. Any kind of help involved is appreciated.
Here is the client code
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#define MAXCOUNT 1024
int main(int argc, char* argv[])
{
int sfd,i;
struct sockaddr_in saddr;
char buff[MAXCOUNT];
char mesg[MAXCOUNT];
sfd = socket(AF_INET,SOCK_STREAM,0);
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
saddr.sin_port = htons(5008);
connect(sfd,(struct sockaddr*) &saddr,sizeof(saddr));
fgets(buff,MAXCOUNT,stdin);
send(sfd,buff,strlen(buff),0);
if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
perror("Nothing to read\n");
exit(1);
}
printf("%s\n",mesg);
exit(0);
}
Here is the server code
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#define MAXCOUNT 1024
int main(int argc, char* argv[])
{
int sfd,nsfd,cn;
pid_t c;
char buf[MAXCOUNT];
socklen_t clen;
struct sockaddr_in caddr,saddr;
sfd = socket(AF_INET,SOCK_STREAM,0);
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
saddr.sin_port = htons(5008);
bind(sfd,(struct sockaddr*) &saddr,0);
listen(sfd,2);
for (; ;) {
clen = sizeof(caddr);
nsfd = accept(sfd,(struct sockaddr*) &caddr, &clen);
if( (c = fork()) == 0) {
close(sfd);
memset(buf,0,sizeof(buf));
cn = recv(nsfd,buf,sizeof(buf),0);
if ( cn == 0) {
perror("Reading from the client socket failed\n PROGRAM CRASH :\n");
exit(1);
}
buf[cn] = '\0';
send(nsfd,buf,strlen(buf),0);
close(nsfd);
exit(0);
}
}
return 0;
}
send(sfd,buff,strlen(buff),0);
if (recv(sfd,mesg,MAXCOUNT,0) == -1) {
perror("Nothing to read\n");
exit(1);
}
printf("%s\n",mesg);
The %s format specifier is for C-style strings, not arbitrary data. And since you throw away the return value from recv, you have no way to know how many bytes you got.
Your client also doesn't shut down the socket gracefully or make sure it has received all the data the server may send. So it's possible that you're triggering an abnormal termination. The server closes the connection when it's done sending, so the client should keep trying to receive until it detects that the connection has closed.