1

In man addrinfo there is example code for an UDP server.

I compiled that code, and run it on a terminal using a.out 9001.

On a second terminal I run netcat 127.0.0.1 9001, hoping that that will open an interactive session with the local server.

And one of the times I tried it did right that, and I could write messages that would be echoed by the server, through in the first terminal the server would not print the received messages as I expected.

But all other times the netcat command just executes and nothing happens. No error message nor output.

This is what there is in the terminal:

jsevillamol@jsevillamol-N551JK:~/Documentos/C$ netcat 127.0.0.1 9000
jsevillamol@jsevillamol-N551JK:~/Documentos/C$ netcat 127.0.0.1 8000
hola
hola
hola
hola
test
test
do not copy me
do not copy me
^Z
[4]+  Detenido                netcat 127.0.0.1 8000
jsevillamol@jsevillamol-N551JK:~/Documentos/C$ netcat 127.0.0.1 9001
jsevillamol@jsevillamol-N551JK:~/Documentos/C$ netcat 127.0.0.1 9002

When I bond to 8000 is when the interactive session seemed to be working.

Also, after stopping the server I cannot run the program again using the same port.

What is going on?

Jsevillamol
  • 2,425
  • 2
  • 23
  • 46
  • 1
    You seem to have multiple problems, that really are unrelated to each other and should be asked as separate questions. Also please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please edit your question to actually *include* the code, as well as the expected and actual output from it. – Some programmer dude Jan 09 '19 at 12:49
  • 1
    Aside from your server code, note that `netcat` assumes `TCP` by default. You might want to try `netcat -u 127.0.0.1 9001` to force `UDP`. – G.M. Jan 09 '19 at 12:59
  • @G.M. That was it! Thank you! – Jsevillamol Jan 09 '19 at 13:18
  • 1
    You might like to have look here: https://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux – alk Jan 09 '19 at 17:25
  • 1
    Also you probably want to implement (a more detailed) error handling and logging. – alk Jan 09 '19 at 17:26

0 Answers0