![]() |
|
#1
|
|||
|
|||
Learning TCP/IP aka TELNET programming but...I am trying to run a server code that will send a simple message to a client once it connects and allow the client to send a string of text to the server and have the server to disply that. The soul reason is to learn about TCP/IP programming WINSOCK and WINSOCK2. I have been pounding the keyboard for a few weeks now and doing online learning from the few places that break it down to a low, no joke its dum simple, code.
Well... my code wont send a string accross the net... 127.0.0.1 3000. The code is as follows: CPP / C++ / C Code:
I am not getting this to send or recieve messages at all. On command prompt it just sticks with connecting to... The source code came from http://www.geocities.com/siliconvalley/2072/sockprog.htm with slight mods made by myself. ... If anyone could help me get the joker chating, a thousand blessings and thanks. Please be extreamly basic in CPP terms. I know just enough to get myself into trouble. Last edited by LuciWiz : 22-Jan-2007 at 01:40.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Learning TCP/IP aka TELNET programming but...Quote:
Have you looked at beej's really (really) excellent web site? http://beej.us/guide I wouldn't dream of trying to learn any network programming without downloading and printing this: http://beej.us/guide/bgnet/output/print/bgnet.pdf In section 6.2: "This program acts like a simple multiuser chat server. Start it running in one window, then telnet to it (“telnet hostname 9034”) from multiple other windows. When you type something in one telnet session, it should appear in all the others." The source for this is "selectserver.c" in this directory: http://beej.us/guide/bgnet/examples/ Depending on what Windows compiler you are using, you just delete the UNIX/Linux headers and put in the <winsock2.h> and the WSA startup stuff. Here's the way I modified the beginning of the file. If I comment out the #define WINDOWS statement, it's the same as beej's original (so I can compile/execute on Linux). The particular changes that I made enable me to compile and execute on my Windows XP platform with various Borland and Microsoft compilers. CPP / C++ / C Code:
If you have already compiled other Windows socket stuff with your compiler, you know whether you have to do something special to link in the wsock2.lib library. (Borland compilers that I use don't require anything extra, Microsoft compilers do.) One reason that your simple UDP server won't work with telnet clients is that telnet protocol involves two ports: Initially connection is made through whatever port the server is listening on. Various telnet-specific control information is transported on this port. Then the server opens a new port for data (the telnet protocol gets this information back to the client) and the telnet client opens a new port according to the server's instructions on the control port. Then all data transfer is through this new data port. (There may be other problems with your program, but I haven't tried to straighten it out --- beej's example works for me, and I like the narrative.) If additional telnet clients try to hook up, the server gives each one a port number for that client's data. SO: The clients do not talk to each other through the original telnet server port; each talks to the server through that client's data port and this particular server sends data from any client port to all of the other clients that are connected. Regards, Dave Footnotes: 1. It's a C program. If you want C++, no changes are required. You can put cout<< instead of printf(), and, eventually, you can make a C++ class to handle socket I/O (using C++ strings instead of arrays of chars; stuff like that). But if your main goal is to learn TCP/IP and Telnet stuff, you can just leave the code looking like C. 2. If you want to use the beej server program "selectserver" and you want to send a "Welcome" message to each new client as it obtains a connection, then find the place in the source where it reports the new connection on the server console, and put something to send the message. Something like: CPP / C++ / C Code:
3. If you want the telnet server to display text from each client as it is received:... (Well, I have to leave something for you to do, right?) 4. If you only want one client to talk to one server, then you don't need telnet. Look at beej's earlier examples of client and server UDP programs. But I see you said that you had already mastered UDP. I think that it still won't hurt to go through beej's examples. 5. The word "cheezy" in the comment is from beej, not me. Since it works as intended and it serves the purpose of illustrating a number of very practical points, I would never call it "cheezy"! 6. Since the title of thread is about "telnet", I am assuming that you are using a telnet client to talk to this server. If you are using something else, let us know what it is. The example from beej works with telnet clients that were installed by default on my Windows systems and my Linux systems as well as with various telnet test programs that I have tried. Last edited by davekw7x : 22-Jan-2007 at 12:31.
|
|
#3
|
|||
|
|||
Re: Learning TCP/IP aka TELNET programming but...Quote:
Each client contacts the server with packets having a destination address consisting of the IP address and whatever port number on which the server is listening. ("Normal" telnet service is on server port 23; our special test server can use any port not specifically being reserved for other services; usually a high number. This is coded into the server application, and the clients must know what server port to use.) On any given machine a client program is granted a unique socket fd for each request. Part of that socket's data information is the destination IP address and port number. The packets through a given socket from each client, therefore, have a unique ip address and a port number as their source address and the telnet server and port number as destination address. Packets from server to a client uses that client's destination IP address and port number. For each new connection, the accept function in the server gets a new fd (a new socket connection) that it uses for data transfer to and from a particular client. Part of the socket's data information is the client IP address and port number, and this is how the server keeps traffic separate for the various clients. There is no separate "control" port from the client's point of view. I recently worked on some ftp problems and that is what was in the almost-remembered/almost-forgotten area of my short/medium term memory and I just got things jumbled up. Kind of like Winnie the Poo's spelling. (See footnote.) With ftp, there are actually separate connections that are negotiated between client and server: a "control" connection that remains constant for the duration of the session, and a separate "data" connection that is negotiated and established for each data transfer. It's more complicated than simple TCP/telnet connection. I regret the error, and I reiterate my exhortation that you should never get all of your information from one source. Don't take my word for it (or any other one person's) as absolute. Regards, Dave Footnote: "My spelling is wobbly. It's good spelling, but it wobbles, and the letters get in the wrong places." ---A. A. Milne "Winnie the Pooh" "Kind of like my telnet explanation: A little wobbly. The words were good words, but some of them just got in the wrong places." ---davekw7x |
|
#4
|
|||
|
|||
Re: Learning TCP/IP aka TELNET programming but...Hey everyone!, I just wanted to say thanks for the help. After many more study hours and about 5am, I was able to compile my first Telnet session with wounderful results. Beej is the best! I once before found that site but wrongfully click off to another because in the body he stated the info was written for linux/unix and he/she had a major dislike of windows.... tisk tisk, I should have kept reading onward anyway.
Well, I have now moved to NON-Blocking/multi-threading. WOW!! so much at one time! Well thats ok too... I'll hack and slash my way through the source, code examples, and gobs of website till I get it figured out. Here is the source code I now have, if anyone else comes along needing an expamle with comments. Compiled with BLOODSHEAD DEVCPP SETUP: PROJECT OPTIONS, LINKER, link to ../lib/libwsock32.a and ../lib/libws2_32.a TESTED USING: zMUD, telnet client for MUDs, and windows 2000 TelNet. Source follows: CPP / C++ / C Code:
The only issue with the code now is that in the WHILE(1) loop the server does not display what the client has typed. However, the server does send data to the different clients. I would have used WHILE(strcmp(buffer, "svrEXIT")!=0) to controll this but then buffer never got the string data... or so it seems. Thus, its still and infinity loop either way. HEY, If you got any comments on my coding style or the posted source, PLEASE be critical. I need to learn good programming habits. Also, If you know any good books I could buy about TCP/IP and/or UDP/IP programming, let me know about that too. Thank again everyone for reading/helping. Last edited by admin : 25-Jan-2007 at 08:58.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|
#5
|
|||
|
|||
Re: Learning TCP/IP aka TELNET programming but...Quote:
...how about good forum posting habits first? In other words, put your code in between [c] ... [/c] tags! ..then maybe we can at least read it? Read the www.gidforums.com for more details. It seems as if you're having a good time discovering network programming. It is a lot of fun. Now you need to get rid of Windoze and start using a real programmer's operating system! :davis: |
|
#6
|
|||
|
|||
Re: Learning TCP/IP aka TELNET programming but...ah, thanks! The first post did it for me.. I was woundering how to make it "set off from the rest of the message body when it wasn't doing right. I'll be sure to use the blocking tags from now on.
|
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Looking for opinions | crystalattice | Miscellaneous Programming Forum | 6 | 27-Sep-2006 21:02 |
| printer / font color / windows programming | nicolas_qc | MS Visual C++ / MFC Forum | 0 | 03-Jan-2006 23:13 |
| which language ? | onauc | C++ Forum | 2 | 19-Nov-2004 02:53 |
| GUI programming | crystalattice | C++ Forum | 5 | 14-Sep-2004 12:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The