LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2013, 01:58 AM   #1
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Rep: Reputation: Disabled
C client socket program in Linux (segmentation fault)


Hi everyone, I wrote c client program in Linux. I got segmentation fault when I run that program. my program is
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 <uniistd.h>
#include <errno.h>
#include <arpa/net.h>

int main (void)
{
	int sockfd, n;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	
	char buffer[256];
	char hostname[1024];
	gethostname(hostname, sizeof(hostname));
	serverINFO = gethostbyname(hostname);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
	{
            printf("ERROR opening socket");
            return 1;
        }
	if(serverINFO == NULL)
	{
		printf("Problem interpreting host\n");
                return 1;
	}
        serv_addr.sin_family = serverINFO ->h_addrtype;
        memcpy((char *) &serv_addr.sin_addr, serverINFO ->h_addr_list[0], serverINFO->h_length);
	
	serv_addr.sin_port = htons (SERVERPORT);
	if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) 
	{
             printf("ERROR connecting");
             return 1;
        }
	printf("Please enter the message: ");
	bzero(buffer, 256);
	fgets(buffer,255,stdin);
	n = write(sockfd, buffer, strlen(buffer));
	if (n < 0)
	error ("ERROR writing to socket");
	bzero(buffer, 256);
	n = read (sockfd, buffer, 255);
	if (n < 0)
	error ("ERROR reading from socket");
	printf("here we are %s\n", buffer);
	close(sockfd);
}
 
Old 10-28-2013, 03:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
how is it related to that post? http://www.linuxquestions.org/questi...am-4175482072/
Have you tried debugging?
Have you got warnings during compilation or build?
 
1 members found this post helpful.
Old 10-28-2013, 03:23 AM   #3
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
how is it related to that post? http://www.linuxquestions.org/questi...am-4175482072/
Have you tried debugging?
Have you got warnings during compilation or build?
Hi, thanks for your ans. I had debugged and solved the problem. Thanks a lot
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Porting a C program to calculate CPU time into Linux: Segmentation fault ramesh333 Programming 5 09-29-2012 03:03 PM
Segmentation fault in client - server programming in Linux ankurcse Programming 10 07-17-2006 09:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration