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

  • SEARCH
  • Home
  • 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 7928843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:52:07+00:00 2026-06-03T19:52:07+00:00

I’m making a client program in C that has to deal with this situation:

  • 0

I’m making a client program in C that has to deal with this situation:

1- server program receives udp datagram in port no 8080 sent by client with a port number X

2- server creates a new socket (TCP) in port number X

3- using this TCP socket, server reads a string sent by the client

(running on localhost)

I don’t need to make the server program, it’s already done. The points 1 and 2 are covered, but I’ve been a couple of days trying to work out the 3rd point and I’m not able to make it work ><

The code I’ve got for the client is this:

#define MYPORT 8080


int main(int argc, char *argv[ ]) {

int sockfd;

/* connector’s address information */

struct sockaddr_in their_addr;
struct hostent *he;
int numbytes;

int sockfd2, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];


if (argc != 3) {
    fprintf(stderr, "Usage: %s <hostname> <message>\n", argv[0]);
    exit(1);
}

/* get the host info */

if ((he = gethostbyname(argv[1])) == NULL) {
perror("Error obtaining the client. \n");
    exit(1);
}

else printf("Client obtained\n");

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("Error creating UDP socket\n");
    exit(1);
}

else printf("UDP Socket done\n");

their_addr.sin_family = AF_INET;

printf("Port: 8080\n");

their_addr.sin_port = htons(MYPORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

memset(&(their_addr.sin_zero), '\0', 8);

 sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd2 < 0) 
    error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}

//sending port where the TCP socket will be associated
//server client connects correctly to this port 
//and the code it's working fine in this point
if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)

{

perror("Client-sendto() error lol!");

exit(1);

}
//port is sent, now let's connect to the port by tcp and write the string
//not working properly from now on

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr,
     server->h_length);
serv_addr.sin_port = htons(atoi(argv[2]));
if (bind(sockfd2,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
    error("ERROR connecting");

listen(sockfd2, 5);
accept(sockfd2, 0, 0);
printf("accepted!\n");

//sending the string to the TCP Port...
if((numbytes = sendto(sockfd2, "hi", 2, 0, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr))) == -1)

{

printf("Client-sendto()-TCP error\n");

exit(1);

}


if (close(sockfd) != 0) printf("Client-sockfd-UDP closing is failed!\n");
else printf("Client-sockfd-UDP successfully closed!\n");
if (close(sockfd) != 0) printf("Client-sockfd2-TCP closing is failed!\n");
else printf("Client-sockfd2-TCP successfully closed!\n");
return 0;

}

The code works for the first two steps, but in the last step, it seems it’s not connecting well with the TCP port, because my client program ends but my server program says that he receives null.
And of course I’m always sending ports > 1024

Thanks in advance, any help will be so appreciated.

  • 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-03T19:52:09+00:00Added an answer on June 3, 2026 at 7:52 pm
    listen(sockfd2, 5);
    accept(sockfd2, 0, 0);
    printf("accepted!\n");
    

    I haven’t read all your code, but the above (at least) is wrong. You absolutely need to retain the return value of accept: it’s the socket you need to write to!

    accept returns a file descriptor for the new TCP socket that has just been created for communicating with the “server” in your case. You need to use that as the file descriptor you write your string to.

    (The sendto call just after that, apart from using the wrong socket, is a bit suspicious since the server will have no way to determine how much data to read/where the message stops. Passing a length of 3 (to include the \0 byte, would be a bit less suspicious.)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.