Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:43:03+00:00 2026-06-15T13:43:03+00:00

I want to establish connection between two pc using client and server using C

  • 0

I want to establish connection between two pc using client and server using C
I have been able to sent data from client and receive by server.
but when server want to sent data the error are appear in server side:
segmentation fail :11

here my program:
in client:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h>
#define MYPORT 4950
#define MAXBUFLEN 100 

int main() {
    char no[16], dt[30];
    printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");
    printf("To : ");
    scanf("%s", no);
    while(1){
        printf("Me : ");
        scanf("%s", dt); send(no, dt); receive();
   } 
} 

int send(char no[], char dt[])
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; 
struct hostent *he;

int addr_len, numbytes;
if((he = gethostbyname(no)) == NULL) 
{
    perror("gethostbyname");
    exit(1); 
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{ //40
    perror("socket");
    exit(1); 
}
their_addr.sin_family = AF_INET;

their_addr.sin_port = htons(MYPORT); 
their_addr.sin_addr=*((struct in_addr*)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)))==-1)
    {
    perror("sendto");
    exit(1); 
}
close(sockfd);
return 0; 
}

int receive()
{
     int sockfd;

struct sockaddr_in my_addr; 
struct sockaddr_in their_addr; 
struct hostent *he;

int addr_len, numbytes;
char buf[MAXBUFLEN];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) //68
{
    perror("socket");
    exit(1); 
}
my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(MYPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero),'\0',8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
    perror("bind");
    exit(1); 
}
addr_len = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
{
    perror("recvfrom");
    exit(1); }
buf[numbytes]='\0';

printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);
close(sockfd);
return 0; 
}

and server:

 #include <stdio.h> 
 #include <stdlib.h> 
 #include <unistd.h> 
 #include <errno.h> 
 #include <string.h> 
 #include <sys/types.h> 
 #include <sys/socket.h> 
 #include <netinet/in.h> 
 #include <arpa/inet.h> 
 #include <netdb.h>
 #define MYPORT 4950
 #define MAXBUFLEN 100
char noip[50]; 
int main() 
{
char no[16];
char dt[30];
printf("‐‐‐‐‐ PROGRAM CHATTING ‐‐‐‐‐\n");
receive();
strcpy (no, noip); 
while(1){
    printf("Me : ");
    scanf("%s", dt); 
    send( dt); 
    receive();
    }
 } 

 int send(char dt[30]) {
   int sockfd;
   struct sockaddr_in my_addr; 
   struct sockaddr_in their_addr; 
   struct hostent *he;
   int addr_len, numbytes;

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
        perror("socket");
        exit(1); 
    }
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(MYPORT); 
their_addr.sin_addr=*((struct in_addr*)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);
if((numbytes=sendto(sockfd,dt,strlen(dt),0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)))==-1)        
        {
        perror("sendto");
        exit(1); }
    close(sockfd);
    return 0; }

int receive() {
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; 
struct hostent *he;
int addr_len, numbytes;
char buf[MAXBUFLEN];
char no[16];

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){
        perror("socket");
        exit(1); }

my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(MYPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; 
memset(&(my_addr.sin_zero),'\0',8);

if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
        perror("bind");
        exit(1); }

addr_len = sizeof(struct sockaddr);
if((numbytes=recvfrom(sockfd,buf,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,&addr_len))==-1)
{
    perror("recvfrom");
    exit(1);}

buf[numbytes]='\0';
printf("%s : \"%s\"\n", inet_ntoa(their_addr.sin_addr), buf);
strcpy(no,inet_ntoa(their_addr.sin_addr));//copy data ke varibel no 
        close(sockfd);
        return 0;
}

Any sugestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:43:05+00:00Added an answer on June 15, 2026 at 1:43 pm

    Do a debug build (option -g) and then call the gdb. It opens a prompt where you use

    file "a.out"
    

    to load the executable and afterwards call “run” to actually run you program under debugger’s control. When the segfault happens, call “backtrace” and it should tell you the function and maybe the line where it happens.

    But when I see the code, my first guess would be the input char array. Do you enter more than 29 characters when sending? 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to establish a connection between my local machine and MySQL database server
I have some java-app, and i want to establish a connection to some https
I need to establish a Socket connection (TCP) between two hosts (say host1 and
I want to establish socket connection to streaming server (with iphone ) and want
I want to establish a connection via Socket to a Server, while the phone
I want to set up an FTP connection using a proxy server with Apache's
I want to create a two-way stream between a Node.JS client and a Node.JS
I have Installed SQL server 2008 I want to connect to it using c#
I have a client-server communication between a mobile and a PC(server). In the communication
I want to establish a connection to OLAP Cube deployed on SSAS from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.