Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / Delphi - ServerSocket And Client Socket Commands.. Delphi 6... Help Please

Author
Message
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 9th Jun 2005 12:04
Hey,
I gave up on the VB6 Winsocket commands because they wouldn't ever work over the internet. Only over LAN.

Anyway I am using Delphi 6 Personal Edition. It has two controls which apparently relate to what I want.
ServerSocket
ClientSocket

How would I set up a Server with the Server Socket and how would I set up a Client with the Client Socket?

Could I possibly get the simplest example of both. Like have a server and a client and when the client pushes the button it sends to the server and the server sends a string that shows up on the clients screen as a message box. Like:
Client sends PING
Server Receives PING
Server sends PONG
Clients has Message box popup that says PONG

I would highly appreciate the help.

Also, if for some reason I shouldn't be using the Socket commands could someone please direct me to the proper command so I can do net communications such as chat. I want it to be able to work over the net and not just locally.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 9th Jun 2005 12:25
winsock should have worked over the internet as well, sounds like a firewall and or router issue.

I cant help on the Delphi stuff, sorry

empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 9th Jun 2005 20:47
Client
To connect to a server:

To send a string:

To receive a message (use the OnRead event)



Server
To set the server to listen mode put the following in the Form's Create event:

When a client connects the Accept event is fired

When a client sends data the ClientWrite event is fired

To send a text to a client (ie. to the 1st client)



Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 01:18
Hmm,
Empy you provide very good help. I do have a question though.

Quote: " ShowMessage(Socket.RemoteAddress + #13 + #10 + Socket.RemoteHost);
"


What do the numbers mean in that line of code? I can't seem to be able to figure it out.

Well I am going to try out this code and I will let you know how it works out.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 01:54
Hmm,
This is where I stand so far.

I implied your sample code you gave me into my real code.
This is what works:
1. The Server Starts To Host
2. The Client Connects To The Server
3. The Server Shows That The Client Connected

I can't seem to get the server or client to be able to send or receive messages. I don't really know which one is the one with the error whether it be receive or send.

Let me post the code for my main form and maybe you can discover the problem. Everything for the Sockets is in my main code except for the connection code. But that doesn't seem to be the problem since it recognizes. The connected code is on a diffeent form.



Please help me if you could on getting my server and client able to send and receive messages.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 06:32 Edited at: 10th Jun 2005 06:45
13 is the ASCII code for "carriage return" and 10 is the ASCII code for "line feed". When you put a # in front of those numbers they're converted to characters (same as the function chr()).

I'll check the code later (as in tomorrow), 'cause I can't test since I don't have Delphi 6 installed anymore and the later versions use different socket components.


Edit:
Looks like they still come with D7 (and prolly D2005 as well- haven't checked) but are not installed by default.
To speed things up, could you attach a zip file of your project?


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 06:55 Edited at: 10th Jun 2005 06:55
Yes ok

What I am trying to do is make a very simple chat program that uses one exe for the project.

Instead of making a server exe and a client exe I would like the user to decide inside the program whether or not he wants to host or join.

If there is a way to do a client to client chat I would rather do that probably. But either way is fine.

Here is that rar file

The size is too big for an attachment so I just uploaded to my site.
http://n.raboy.home.comcast.net/download/Chatterbox.rar

Mess With The Best, Die Like The Rest.
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 10th Jun 2005 07:03
This is probably of no use to you - it is just something I thought of when you mentioned sockets and clients...

There is a component library that you can use which is open source and freely availible. I don't know if they are distributed with Delphi 6 PE, but perhaps you could look it over - the library is very good, and abstracts a lot of the things you otherwise have to do with sockets.

The library is called Internet Direct. With it you'll get anything you could possibly ever need (and things you could probably not ever need).

You can find it here:

http://www.indyproject.org/

The version you want is Indy 8, and not the later ones which are for later Delphi compilers.

Quote: "

Information from the Indy site:

"Do the following:

1. Download the Indy 8.0 distribution at http://www.nevrona.com/Indy/dowload/80.html along with the updated Delphi 6 Files.
2. Run FULLD6.BAT from your Indy\Source directory
3. Place the DclIndy60.bpl in your Indy\D6 directory in the IDE ( Component|Install Packages|Add).
4. Go to Tools|Environment Options...|Library and add your Indy\D6 directory to the Library Path."
"


-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 07:46
@MicroMan Yeah the indy components is what I use too, they're pretty fast and stable.


@The Lone Programmer
There was a little mistake on my side and one on yours.
My mistake was that not ClientWrite but the ClientRead event is fired when the server receives a message.
Your mistake was that you didn't assigned the Event procedures to the client and server objects in the Object Inspector.


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 09:21
Hmm,
How do I assign event procedures? I am new to Delphi and my only real reason for using it right now is to see if the net commands will work for me in it.

I really don't think they are going to work because I tried the program with someone over the net and it failed to work. When I ran it locally and connected to myself, my server part detected a new user joining. This failed to happen over the internet.

Are the ServerSocket/ClientSocket commands the same as the VB Winsock Commands? Because they appear to be very similar. I already know that winsock commands will not work for me.

I tried to download and install Indy but either the directions I got were bad or it wont work with personal edition.

Please keep helping me, I apreciate it.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 09:27
Quote: " Hmm,
How do I assign event procedures?"

Object Inspector -> Events
You know the same way you assign stuff to a Button that should happen when the User clicks it. So that Delphi knows which object should to what.


Quote: "I really don't think they are going to work because I tried the program with someone over the net and it failed to work. When I ran it locally and connected to myself, my server part detected a new user joining. This failed to happen over the internet."

Provided you've got the correct IP address(es) and the correct firewall/router settings, it does work over the internet.


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 09:41
How come MSN Messenger or AIM doesn't have you change up your router settings? Does Delphi has a Net Command System that works like a real online program. One that doesn't have you change a whole bunch of stuff?

I downloaded and successfully installed Indy and I'm trying to figure it out. If you guys could give me guidence I would appreciate it.

Or can you guys construct and compile a cheap little chat program that you know for sure works over the internet, and then let me try it out. My hopes are almost gone, and I need some confidence that a net program will work with anything.

What does Dark Basic use? I have never had a problem making a chat program in Dark Basic and have it work over the net.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 09:52 Edited at: 10th Jun 2005 09:55
Try to connect to me at 217.85.8.93. I'm there till 0:15am GMT (20 minutes).


Ooops wait a second, what port do you use in your app?


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 10:02
Um I was using port 1514

Am I connected to you with Sockets or the Indy methods?

Do I build with Indy in a similar fashion to how I built with the Sockets?

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 10:03
Try the sockets.


Play Nice! Play Basic! Version 1.073
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 10:09
Well you connected


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 10:10
I don't know how I am going to tell if I am connected.. I tried to connect to you and I got no error. Thats all my program does at the moment.

Does your server say I connected to you?

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 10:10
Yes that's why I said " Well you connected".


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 10:22
So I really connected.

Then how come mine wouldn't work?

I hosted mine or I had someone else host mine and then we connected.. It didn't say anyone connected like it would if I tried it with myself.

I am confused.

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Jun 2005 10:26
Well I don't know why it didn't work.
Try to modify the source the way I suggested it, and then perhaps we can try it again tomorrow... and actually chat.


Play Nice! Play Basic! Version 1.073
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 10:30
How would I make a chat program using the Indy methods?

I setup my code as normal and I am successfully able to make a server and connect to myself.

Now with that said and done, what commands with Indy are used to send and receive stuff? And do I need some kind of event procedure?

Indy looks a little bit easier.

Any advice?

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 10th Jun 2005 11:52
argh...

I got everything working over the LAN (Locally on my computers), but it still wont work over the net.

It looks like it wants to work but I keep getting a socket error when I try and connect to the host. Any Idea's?

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 10th Jun 2005 17:01 Edited at: 10th Jun 2005 17:03
Edit: Never mind the following... I should learn to read the WHOLE thread before I open my mouth.

---------> Original post

A smart way to check if a program connects is to use the IP-number 127.0.0.1

So, you set the server to listen for a port, and then set the client to transmit on that port. This requires you to have two instances of your program running at the same time, one set to be a client and the other set to be a server.

When you use 127.0.0.1 you will always target your own machine, the localhost. Using the localhost address you can test the machinery of your program and see whether the client transmits and whether the client listens.

You are going to have more trouble trying your software on the real net due to routers, and so on. That's just a level of abstraction you don't need when you start out with sockets.

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 11th Jun 2005 01:22
Is there any kind of component that doesn't need you to worry about routers?

MSN doesn't make you worry about routers and same for AIM. Same situation follows with online games such as Counterstrike and Everquest. None of which require you to take any extra steps on your part. Just connect, and possibly click allow on your firewall. Nothing more.

Empty:
You said I successfully connected to you. What makes your server so special, in making it work? Why could I connect to you and no one else and why couldnt anyone connect to me? I tried both the Indy Methods and Socket commands and neither are working for the Net. Can you let me see your server code, or code me a fresh server and client that you have tested and know for sure work over the net.
I need a confidence boost that by coding for the net that I am not just wasting my time.

What kind of connection command does Dark Basic use? I have never had problems using the Net with Dark Basic? Why is that any different then my situation.

I strongly believe that it is not a router issue with me. And if it for some strange reason it is, then there must be a work around without actually changing the settings on the router.

Please post back.
Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 11th Jun 2005 03:12
I basically used your app (just with the changes I mentioned.
Anyway I've attached slight improved version of it.


Play Nice! Play Basic! Version 1.073

Attachments

Login to view attachments
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 12th Jun 2005 10:30
Once again, thanks for all the help guys.

I successfully got it working now. I thought I would go with the Indy stuff, but I am confused on how I am suppose to send and receive on both the client side and server side.

I can connect the client to the server perfectly, I just don't know the proper ways to send and receive.

If you guys can continue to help I would greatly appreciate it.

Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 13th Jun 2005 09:10
Quote: " Is there any kind of component that doesn't need you to worry about routers? "


No, not really because a component is only concerned with connecting and transmitting. It does not care how the data is transported across the net.

Routers make things difficult no matter what component you use or write. You just have to write code that polls the router to get your correct IP number, and not the local IP that the router sets up for you (which is meaningless outside your network).

How do you do that? That's a complex question, and you'd best look into the SNMP protocol (that followed with the Indy-library) for that! SNMP allows you to communicate with network devices on the network, such as the router.

There's no getting around the fact that routers make your coding life more difficult, and rather than butt you head against that wall I'd suggest you concentrate on making the app for local lan to learn the basics. Then when you've gotten it to work, you can move onto SNMP and such.

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 13th Jun 2005 21:57 Edited at: 13th Jun 2005 21:58
Hmmm, I was thinking about this, to try to come up with a simple and elegant solution to at least resolving your external ip. Getting the router information IS hard: you either have to create a whole app to poll its information by HTTP or telnet.

After complicating and prevaricating, looking at the most bizarre ways of doing this, it struck me. Even if you can't look out of your LAN with your app, your webbrowser can! Otherwize you wouldn't be able to connect to TGC, would you.

So, the solution lies in using the existing internet capability already on your computer. You can send an HTTP message to a site and then read its response to find your correct address. What better site to poll than http://whatismyip.org?

You need to put a hidden TWebbrowser-object in your app, and when you start up your app you need to check the IP, and then store it in the app. Hopefully you have the TWebbbrowser-component - otherwise you have to load the SHDOCVW.DLL on your own and create a wrapper for it.



Now all you have to do is store the data that comes back, after parsing it, of course.

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
The Lone Programmer
22
Years of Service
User Offline
Joined: 29th Jan 2003
Location: California, USA
Posted: 14th Jun 2005 01:32
How would you pull in just your IP from that site? Is there a special code that translates and kills off all the unneccessary junk that comes in?


Thanks,
The Lone Programmer

Mess With The Best, Die Like The Rest.
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 14th Jun 2005 02:20
You would have to locate the correct spot for the IP address, and cut it out of the whole HTTP response. The easiest would be to scan the HTML-code in the document that you receive. You have the Delphi versions of VBs Mid(), Left() and Right() functions in the StrUtils unit. Just includ that in the uses clause at the top of the page.

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----

Login to post a reply

Server time is: 2025-06-06 16:40:19
Your offset time is: 2025-06-06 16:40:19