11 Replies - 974 Views - Last Post: 23 December 2015 - 07:14 AM Rate Topic: -----

#1 general656   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 288
  • Joined: 25-March 15

C Network Programming - Getting Client Input ?

Posted 20 December 2015 - 07:16 AM

Hey guys,
I just started yesterday Network Programming. Well I did the basic things like sending a message to the client who connected to the server. But how do I get to the point of receiving actual input from the Client/User ?

I tried recv/send in this way :
Server :
    recv (listenfd, charbuff, sizeof(charbuff), MSG_PEEK); // Receive input
      printf("%s\n", charbuff); // Print input for debug reasons
      write (connfd, charbuff, strlen(charbuff)); // Send back the input for debug reasons


Client :
  char mystr[256];
  fgets(mystr, 256, stdin);
  send(sockfd, mystr, sizeof(mystr), MSG_PEEK);


But nothing really happened ... I thought it works like that but as it seems it doesn't. Can anyone explain to me how can I achieve that and how it actually works ?

Is This A Good Question/Topic? 0
  • +

Replies To: C Network Programming - Getting Client Input ?

#2 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C Network Programming - Getting Client Input ?

Posted 20 December 2015 - 09:12 AM

You would connect in a loop with the accept function then

use the return value from recv and if it doesn't equal SOCKET_ERROR then print out the contents of your buffer.


Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 general656   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 288
  • Joined: 25-March 15

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 02:10 PM

View Postsnoopy11, on 20 December 2015 - 09:12 AM, said:

You would connect in a loop with the accept function then

use the return value from recv and if it doesn't equal SOCKET_ERROR then print out the contents of your buffer.


Best Wishes

Snoopy.

Snoopy I wish that would be helpful brother, but it's like I want a whole recipe and you're giving me only one ingredient.

I want something more like how read/write works, how can I make it like a chatroom (Yeah it's not that hard I suppose ... I think it's only a loop of read/write and checking the amount of bytes they return). It looks simple e.g. Write sends a char and read gets a char. But as I saw it isn't. And how much recv and send differ from read and write ?

As you saw I have a whole hole in the topic. :) Thank you though, have good Christmas if you already gone for holidays !
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14566
  • View blog
  • Posts: 58,407
  • Joined: 12-June 08

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 02:28 PM

Quote

but it's like I want a whole recipe and you're giving me only one ingredient.

Please be cognizant of the rules regarding asking people to do your work for you.
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1856
  • View blog
  • Posts: 6,682
  • Joined: 19-February 09

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 02:39 PM

Hi, is a cookbook ok?

Are you aware of :

Beej's Guide to Network Programming

You possibly need to show more code - a program, for us to look at.
Was This Post Helpful? 0
  • +
  • -

#6 general656   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 288
  • Joined: 25-March 15

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 04:05 PM

View Postmodi123_1, on 21 December 2015 - 02:28 PM, said:

Quote

but it's like I want a whole recipe and you're giving me only one ingredient.

Please be cognizant of the rules regarding asking people to do your work for you.

I said "Thank you brother" in a way (I mentioned "Brother" to look as friendly as I could so there wouldn't be misunderstandings as this ...[!]).
I didn't ask anything from him I just said that he wasn't helpful enough in a kind way. Guys don't be offensive and easily offended and don't consider that "people ask things from you" when they do some critic in a kind way. I think we all need someone to judge us in a kind way, don't we ? Or else we'll never get any better at anything. You can judge me as well, I don't consider myself as a Master. And also your judgement (like mine was) may be wrong. End :P

Although what I wanted to say is that I found the way and it's THAT simple as he described it, I just didn't configured it right. So he was right when he described it that simple because it is that simple, I just thought that it's not. That's all ... :)

< I edited the post many times so I can explain as clearly as I can my intentions>

View Post#define, on 21 December 2015 - 02:39 PM, said:

Hi, is a cookbook ok?

Are you aware of :

Beej's Guide to Network Programming

You possibly need to show more code - a program, for us to look at.


Thank you man. You won't have to read a bunch of code because I finally understood all the concept around it and it's more simple that I thought. Though, thank you for the Beej e-book :D

This post has been edited by general656: 21 December 2015 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 04:43 PM

It's been a long time since I've done any TCP/IP coding. As I remember, you need to setup a listener socket that will listen for incoming calls. The listener then tells the client to call the server back on another port and hangs up allowing it to take another caller and when the client calls back on the other port the connection is established.

After that it's pretty much just a byte stream. You put bytes in the send buffer and they magically appear exactly the same in the other computer's receive buffer. You probably have to read the bytes out to get more bytes into the buffer. It's just one long continuous stream of bytes that could contain whatever you like.

That MSG_PEEK looks suspicious because generally when you peek at something you don't alter it. So, it's likely not removing the data from the buffer to make room for more incoming data.

Also, Telnet is your friend when debugging a server. It will send whatever you type to the server allowing for easy testing with a client that you know actually works as opposed to a client you made that may or may not work. Once you get your server working properly, you can then use that to test your client software.

Note that it does not necessarily automatically show on the screen what you type. Telnet sends what you type to the server's receive buffer, but it does not by default put that on the screen. You have two options. An easy one when you're building your first server is to have the server echo back the text sent to it. Anything sent to Telnet from the server will appear on the screen. And this helps you know you have a good bidirectional connection. Or you can change a telnet setting that echos what you type to the screen which is maybe desirable once you get past the initial echo server tests.
Was This Post Helpful? 1
  • +
  • -

#8 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C Network Programming - Getting Client Input ?

Posted 21 December 2015 - 08:20 PM

general656,

I tried to be as clear as possible with my post I wasn't being flippant.

If you had posted more code I could have posted more code,

that is the way it works round here, so in future help us to help you.

I am glad you figured it out,it really is as simple as I described it.

If you have your ipv4 address and those basic concepts, it works.

Even now if you post your client and server code that will enable me to post my version of a client and server code

so you can see the comparison.

Best Wishes

Snoopy.
Was This Post Helpful? 2
  • +
  • -

#9 general656   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 288
  • Joined: 25-March 15

Re: C Network Programming - Getting Client Input ?

Posted 22 December 2015 - 02:26 PM

mod: removed giant quotes

@snoopy11-

Thank you Snoopy :)/> :)/> You're very minimalistic in a good way and you make simple post, I start to like it I could say.
Best wishes as well.

@BBeck-

Hey, you've been really resourceful and informative ! :D/>
But when I'm writing code ... what exactly do you mean "use telnet" ? I'm not very much informed about Telnet.
So my question is ... do I already use Telnet ? Because it stands for TELecommunication NETwork. I'm new in Network Programming & the whole stuff. But I already know that there's a safer network called SSH which stands for Secure Shell, but ... How do I program a Secure Shell ? I mean if I program now a Telnet, can't I program an SSH as well ?

This post has been edited by modi123_1: 22 December 2015 - 02:31 PM
Reason for edit:: please use the 'reply'

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14566
  • View blog
  • Posts: 58,407
  • Joined: 12-June 08

Re: C Network Programming - Getting Client Input ?

Posted 22 December 2015 - 02:30 PM

Did you try searching "Telnet"?

https://en.wikipedia.org/wiki/Telnet
https://technet.micr...y/bb491013.aspx
http://www.telnet.org/htm/faq.htm

PS - there's no need to quote the giant posts above yours. You can simply reference the person's name.
Was This Post Helpful? 1
  • +
  • -

#11 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: C Network Programming - Getting Client Input ?

Posted 22 December 2015 - 08:55 PM

You server should have a single listener socket. This socket however does not ever communicate with clients. Its only job is to listen for incoming connections. When a connection is being attempted, simply call accept() and pass the listener socket to the function as a parameter. The accept() call will return a newly created socket which is meant to communicate with the client.

You can use FD_SET (google that) to determine when a client is attempting to connect, or if already connected clients are sending information.
Was This Post Helpful? 1
  • +
  • -

#12 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: C Network Programming - Getting Client Input ?

Posted 23 December 2015 - 07:14 AM

Telnet is a program. It comes built into Windows and probably Linux I would imagine. There are several old standards, protocols, and programs that go back to the early days of the Internet like FTP, SMTP, and such. Telnet is a generic "dumb terminal" software.

It used to be that all you had to do was go to a console window and type "Telnet" in. I just tried it on Windows 7 and it did not work. I think you have to go to Control Panel=>Programs=>Programs and Features=>Turn Windows Features on and Off and then select the Telnet Client check box. Then you can probably go to the console window and type Telnet followed by an IP address and a port number. You can use URLs also.

When you play with it a bit, you'll quickly discover it's really a very thin client. You could probably build the same thing yourself in about 4 hours. For the most part, all it does is send whatever you type to whatever server is on the other end of the connection and display whatever that server sends to your side of the connection. There are a couple of other features I think like keyboard echoing.

I saw they have a Telnet server in Windows 7 as well. I'm not sure what that is but it might be good for testing clients maybe. Telnet client is definitely good for testing servers.


FTP and SMTP are relatively easy protocols to understand and learn. Their protocols are well documented on the Internet. Making an FTP or SMTP client can be a pretty good student project when learning TCP/IP programming.

SSH is more intermediate level. Work on easier things first. I tried writing an SFTP client after writing an FTP client and I failed because the server I was testing against did not seem to follow the published protocol. I eventually just gave up, but it's much more difficult than writing an FTP client.

With SSH you are getting into security and encryption. That's great, but it's a more advanced topic.

Also, before you try and program SSH, you should use it a few times to get familiar with what it is and how it works. SSH is more of a Linux thing. There it's pretty much built in and easy to use. In Windows, I've always had to download software to work with SSH. Same for SFTP.

Before you do that though, work on some easier projects like an FTP client and maybe even an FTP server. Or learn to spoof emails with SMTP.

I have a great story I love to tell about when I figured out SMTP. I wrote a program that would send an email via SMTP. But I also figured out real quickly that I can label it as being sent by anyone because nothing checks to verify the sender is who they say they are. So, I send an email to one of my coworkers sitting across the room where I can see his face from our boss that says "You are fired. Pack your things and go!". His eyes got really big when he opened that up. I let him suffer for about 1 second before letting him in on the joke. I didn't want to let it go too far. I gave him just enough time to read it and I was watching him the whole time.

Those old protocols are easy to learn but highly unsecure. SSH and SFTP are secure because they use authentication and encryption which are fairly complex topics on top of trying to learn TCP/IP and basic protocols.

This post has been edited by BBeck: 23 December 2015 - 07:28 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1