2 Replies - 595 Views - Last Post: 29 October 2008 - 03:10 PM Rate Topic: -----

#1 javalurnin  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 05-October 07

sockets

Posted 29 October 2008 - 01:37 PM

I'm working on a simple client-server chat program using sockets. Both the client and the server compile fine, but my server program keeps getting a segmentation fault. I have attached the code.

Please help! I'm lost with this problem!

#include "../lib/my.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h> /* I included this and the following 2 because I kept getting the error:\
warning: implicit declaration of function... */
#include <stdlib.h> /* these includes have fixed that error when I use -Wall */
#include <unistd.h>

int main(int argc, char **argv)
{
int listenfd;
int connectfd;
int n;
int portnum;
char msgBuf[1024];
struct sockaddr_in servaddr, cli_addr;
socklen_t clilen;
pid_t pid;

listenfd = socket(AF_INET, SOCK_STREAM, 0);

memset(&servaddr, 0, sizeof(struct sockaddr_in));
portnum = atoi(argv[1]);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(portnum);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

listen(listenfd, 5);

while(argc >= 2)
{
clilen = sizeof(cli_addr);
connectfd = accept(listenfd,(struct sockaddr *)&cli_addr, &clilen);

if((pid = fork()) == 0)
{
close(listenfd);

for(;;)
{
n = recvfrom(connectfd, msgBuf, 1024, 0,(struct sockaddr *)&cli_addr, &clilen)\
;
sendto(connectfd, msgBuf, n, 0,(struct sockaddr *)&cli_addr, sizeof(cli_addr))\
;
printf("-------------------------\n ");
msgBuf[n] = 0;
printf("Hello, I got the message: \n");
printf("%s", msgBuf);
printf("-------------------------\n");
}
}
close(connectfd);
}
return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: sockets

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: sockets

Posted 29 October 2008 - 02:17 PM

First thing that jumps out at me is YOU DIDN'T USE CODE TAGS!
[:code:]

You also don't check to see if any your socket functions fail.

Have you checked out Beej's Guide?
Was This Post Helpful? 0
  • +
  • -

#3 javalurnin  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 05-October 07

Re: sockets

Posted 29 October 2008 - 03:10 PM

View PostJackOfAllTrades, on 29 Oct, 2008 - 02:17 PM, said:

First thing that jumps out at me is YOU DIDN'T USE CODE TAGS!
[:code:]

You also don't check to see if any your socket functions fail.

Have you checked out Beej's Guide?



First of all... NO NEED TO YELL ABOUT THE CODE TAGS! This is my first real post. Everyone makes mistakes.

Second...The bold type is what I just added. The seg fault is now gone, but I have another question; do I need to actually define a port? I thought my portnum did that for me?

#include "../lib/my.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h> /* I included this and the following 2 because I kept getting the error:\
warning: implicit declaration of function... */
#include <stdlib.h> /* these includes have fixed that error when I use -Wall */
#include <unistd.h>

int main(int argc, char **argv)
{
int listenfd;
int connectfd;
int n;
int portnum;
char msgBuf[1024];
struct sockaddr_in servaddr, cli_addr;
socklen_t clilen;
pid_t pid;

[b]if(argc < 2)
{
perror("Socket error! ");
exit(0);
}[/b]

listenfd = socket(AF_INET, SOCK_STREAM, 0);

memset(&servaddr, 0, sizeof(struct sockaddr_in));
portnum = atoi(argv[1]);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(portnum);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

listen(listenfd, 5);

while(argc >= 2)
{
clilen = sizeof(cli_addr);
connectfd = accept(listenfd,(struct sockaddr *)&cli_addr, &clilen);

if((pid = fork()) == 0)
{
close(listenfd);

for(;;)
{
n = recvfrom(connectfd, msgBuf, 1024, 0,(struct sockaddr *)&cli_addr, &clilen)\
;
sendto(connectfd, msgBuf, n, 0,(struct sockaddr *)&cli_addr, sizeof(cli_addr))\
;
printf("-------------------------\n ");
msgBuf[n] = 0;
printf("Hello, I got the message: \n");
printf("%s", msgBuf);
printf("-------------------------\n");
}
}
close(connectfd);
}
return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1