Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#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];
struct hostent *h;
gethostname(hostname, sizeof(hostname));
server = gethostbyname(hostname);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
if(server == NULL)
{
fprintf(stderr, "Error no such host\n");
exit 0;
}
bzero((char ) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr, server->h_addr, server->h_length);
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons (SERVERPORT);
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0)
error ("ERROR connecting");
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);
}
Hi everyone, I am a bit newbie and Linux. When I run above program, I got an error like "segmentation fault". Why it is so? if anyone knows, pls kindly guide me.
there is no such error (like segmentation fault).
Try to use debug info, debug your code step by step or try to use valgrind to catch the problem. You can even try to analyze the coredump.
there is no such error (like segmentation fault).
Try to use debug info, debug your code step by step or try to use valgrind to catch the problem. You can even try to analyze the coredump.
Hi thanks for your reply. But how to use debug info. Now I am using Geany as my ide. I am a bit new to all those. So sorry for my stupid question. And thanks for your understanding...
that is ok, -Wall -g should also work. Surely it will not solve the issue but will generate additional information to help to solve this problem.
you will have a result, a program and you can try gdb <your program> to debug it. (better to try ddd, it has a nice gui)
but first you need to check if the compilation/building gave you warnings and you need to try to eliminate them.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.