need to get started on sockets
need to get started on sockets
need to get started on sockets
need to get started on sockets
need to get started on sockets
need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets
need to get started on sockets need to get started on sockets
need to get started on sockets
Go Back  Xtreme Visual Basic Talk > > > need to get started on sockets


Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2007, 05:13 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default need to get started on sockets


sockets, the final frontier.

i know nothing about sockets or servers.

i've seen incomplete code, ...i don't get it

i've seen links to sockets with all the socket functions ,...i don't get it


a very simple WORKING example would be nice
__________________
The Pho·net·ic Programmer
Reply With Quote
  #2  
Old 07-19-2007, 12:07 AM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

__________________
The Pho·net·ic Programmer
Reply With Quote
  #3  
Old 07-19-2007, 12:09 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

What do you think you want to do with them ? It's a bit difficult to advise without some sort of Specification
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #4  
Old 07-19-2007, 08:48 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

i need to make a client program and have it connect to my game program
__________________
The Pho·net·ic Programmer
Reply With Quote
  #5  
Old 07-20-2007, 01:21 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Where to begin ?

I'd start off by defining the Application Protocol that you will use to communicate between Client and Server. This is basically a set of 'rules' that each will keep to so that they understand each other.

Let's assume you are sending textual data to and from the client(s) / server. You will need to define an End-Of-Record (eor) character that will always be at the end of a logical record (so you can successfully unblock the data stream). No doubt the Client will be sending 'Commands' and associated data to the server which will reply accordingly, so you may also wish to create a Field Seperator (fs) to make life easier when it comes to unblocking Commands and data.

For instance:
MOVE<fs>Data1<fs>Data2<fs>Data3<fs>Data4<fs><eor>
might be a command from the Client to the server. When the server has unblocked the record it's a fairly simple matter of using the Split function (on <fs>) to unblock each data item.

Next I'd define the 'Commands' that will be sent / received. I don't know anything about your game but let's say we have a 'shoot-them-up' sort and you may have commands like MOVE, STOP, TURN, FIRE. So for each Command there may be data elements (eg Move from x,y to x1,y1) so x,y,x1 and y1 would be individual fields within the record, as described above, and you know that the MOVE Command always expects 4 arguments. FIRE may have more or less eg FIRE<fs>weapon<fs>direction<fs>strength<fs><eor>.

Once you've gone through those steps you've basically defined the 'language' and 'grammatical rules' that the Client and Server will use to communicate.

Now you can start to think about implementation. As a beginner with Comms., I'd recommend you use the Winsock Control rather than the Winsock APIs.

One of the things you're going to have to get your head around is that networky things happen asynchronously and you're not really in total control. As data traverses a Network many things can happen to it, especially important is to understand that when you send a logical record it wont necessarily all appear at once at the receiving end.

I use a Garden Hosepipe as an analagy, one end is connected to the water source and you're holding the other end. Whilst the water is coming out of the source in a continual stream, the local kids are riding their bikes over the Hosepipe so that the water is coming out at your end in spurts. You can consider the Hosepipe to be the network, the water as your data and the kids on their bikes as the various 'interruptions' caused by the devices within the network (Routers, Bridges etc) which are essentially routing and perhaps re-blocking your data. Just like the water, all your data will eventually arrive and it will be in the same order as it was sent. (Unlike the kids on their bikes, you can't stop the network from doing its thing)

In Winsock terms this means that when you issue a SendData, the receiving end may receive none, one, or more DataArrival events. Also when the GetData method is issued, one, two or more, or a partial record may be received, so your code has to collect the data as it arrives and check to see if a complete record has arrived. If one has then it can be processed, then the buffer has to be checked again in case there is more than one complete record or there's a partial record left in the buffer which might or might not be completed on the next DataArrival event.

HINT: Don't go anywhere near DoEvents in Winsock Applications

Getting down to the nitty gritty, as with any conversation, someone has to be listening and someone has to be speaking. The same applies to your Client and Server. Normally the Server will start off listening, in the case of TCP/IP the Server will be at a given IP Address and be listening on a given TCP Port (Winsock Listen method). In order to start the conversation the Client has to connect to the Server by specifying the IP Address of the Server and the TCP Port number it's listening on and request a connection.
(Winsock: RemoteHost = "IP Address", RemotePort = TCP Port Number, Connect method)

This request causes an event to be raised at the Server (ConnectionRequest event) and the Server will decide whether to Accept the connection or not. (Like the 'phone ringing, you decide whether to receive the call or not).

(Winsock AcceptRequest method)

Once the Server Accepts the connection, a Connected event is raised in the Client. Once the client recognises this event then it knows that the connection is established.

Once the connection is established the conversation can begin, either the Client or the Server can start it - perhaps the Server sends a welcome message and then the Client logs on, or something.

One other thing to remember is that it's possible for a connection to be broken at any time through things like; network failures, the Client disconnects and goes for tea, etc etc. Just because you were connected the last time you sent something, doesn't mean that you'll still be connected the next time you send something - you have to check - there is a Winsock.Close event which triggers when a 'normal' close happens, but some network breaks will not cause that event.

In the case of multiple simultaneous Clients, it's normal to use a Winsock Control Array and dynamically load new elements of the array as connection requests from different Clients are received. Again normally, element zero of the control array is the Listener and elements 1 to n are used to communicate with Clients 1 to n.

There's quite a few learned posts on Winsock, Client / Server applications, including some examples, in the Communications Forum. I'd do a search using Winsock and take particular note of dilletante's posts. (Some of my diatribes may also be worth a read)

Good luck, no doubt you'll be back with some more questions once you've made a start.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data

Last edited by DougT; 07-20-2007 at 03:21 AM.
Reply With Quote
  #6  
Old 07-20-2007, 02:43 PM
dilettante's Avatar
dilettanteneed to get started on sockets dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

Learned? Well hard learned maybe.

DougT is right though, there are a lot of threads here discussing the mechanics of using the Winsock control for TCP and UDP to couple clients to a server. He's probably also right to suggest that the mechanics are a separate issue from deciding how you want the clients and server to communicate.

You need to develop some sort of division of workload between the client and the server. Once you have divided up the labor you'll have a better idea what sorts of things the client needs to request and what the server will respond with. Then you can devise some sort of command language that defines each type of request/response and its parameters.

At the simplest level the server almost becomes more like a MUD or other multiuser text-based game. Clients translate user actions into textual commands they "type" to the server, and translate responses "printed back" by the server into changes in the user interface display, local statistics, etc.

The actual form of the client/server exchanges can range from human-readable text to XML to some binary format. This isn't so important to the process though, and you can pick almost anything as long as it is flexible enough. Most IM systems rely on messaging derived from the IRC standards, which are text-based. The HTTP message formatting used by web servers and browsers is also text based. I think you might want to start with something simple like this.


A lot depends on your game. If it is something very graphical and driven by a joystick you could have the client be fairly dumb, and just take joystick movements and button clicks and send those, and the server would response back withcommands to update the display. That gets pretty slow though, sending tons of stuff over the network and requiring the server to do a lot of processing for all the users.

A smarter client might handle most of the local display updating in response to joystick input and only send the server things at a higher level. For example "I am now at (X, Y) and have fired a type 3 missile on a 243 degree heading." Of course the server must in turn relay or broadcast such facts to the other users' clients so they can update their own displays, and so on.

There is also the issue of whether client code is to be considered trustworthy in order to prevent illegal moves via a hacked client program. For example you might not trust the client to keep track of how many of those "type 3 missiles" he has left.


But maybe you have all of this down already. Most of it is probably more of a Gaming forum issue than a Communication issue.

DougT already gave a good overview of the lower level mechanics. To help you more we'll probably need more specific questions once you've tried working with a small pair of programs. Lots of threads here have small samples of things like a chat client and server. Maybe that would be a place to begin.

Try searching the Communication forum for something like "chat server" and you should find a number of them.
Reply With Quote
  #7  
Old 08-12-2007, 07:15 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

ok so i found what i was looking for , the file attached.
a very basic use of sockets.



now.............................

if i'm making a 2d graphical game, am i going to sending tile information in things like arrays, or will i be sending everything in text format?

(i've already made a basic coder/decoder for text.)
Attached Files
File Type: zip SimpleClientServer.zip (2.9 KB, 21 views)
__________________
The Pho·net·ic Programmer
Reply With Quote
  #8  
Old 08-12-2007, 08:39 PM
dilettante's Avatar
dilettanteneed to get started on sockets dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

Everything sent via the Winsock control goes as a stream of bytes. If you send anything besides a Byte array, it goes through a conversion process (for example String variables and literals are converted to a sequence of ANSI bytes). Send anything more structured (even an Integer array) results in serialization to a sequence of bytes.

In most cases you'll want control of serialization yourself, mostly because you typically need to add some sort of framing to break the TCP byte stream into logical units (such as records or messages or whatever you want to call them), but also because your sender may need to tag the message with some sort "message type" indicator so the receiver knows what to do with it.
Reply With Quote
  #9  
Old 08-16-2007, 08:17 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

thanks for responses.


i'm going to start off using two characters , one to start statement, one to end. (and a statement that isn't full will abort)



does the IP adress have to be exact , or is that just standard programming that can change?

if so , how does the IP work, checking against the program and the web site?
(because the sample i provided can use any number ,although it's not a real internet program)
__________________
The Pho·net·ic Programmer
Reply With Quote
  #10  
Old 08-16-2007, 11:33 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Not quite sure what you mean by "exact". In order to create a TCP/IP connection you need to have both a valid IP Address and a TCP Port

eg
192.168.0.1 could, perhaps, be the Server IP Address and the Port coulld be 40500. In order to establish a connection, the Client must connect to 192.168.0.1 and Port 40500.

If you're going to run this over the Internet then there's a slight complication unless you've got a static IP Address. I suspect, like many, your IP Address is dynamically allocated to you by your ISP every time you connect. Either you have to somehow tell your clients what the IP Address is every time it changes or you can use a DNS (Domain Name System) entry.

At the very highest level, you can think of DNS as being a huge Look-up Table, where every IP Address has a corresponding name. There are many sites on the Interent that offer the facility to create your own DNS entry and to update it as required. (for free)

As an example, I use http://www.no-ip.com/ which allows me to have a DNS entry such as 'dougt.no-ip.info' which 'points' to my current IP Address. In my Client / Server programs I connect to the DNS name which gets 'automatically translated' to the IP Address when the programs are run.
eg.
Code:
Winsock1.RemoteHost = "dougt.no-ip.com" Winsock1.RemotePort = 40500 Winsock1.Connect
When run, the DNS searches for an entry "dougt.no-ip.com" and the program uses the IP Address corresponding to the DNS entry.

The final link in the chain is that I have a program, that uses the WebBrowser control, which runs automatically when my Internet connection is established and updates my DNS entry with the dynamic IP Address allocated by my ISP.

Of course, if you're going to use a (physical) server provided by someone else, then they'll give you a DSN name and take responsibility for maintaining it. If you are planning on doing that, then be sure to check that they'll allow you to deploy and run your program - a lot of providers only allow http (ie Web based programs) as opposed to TCP/IP Client / Server programs and, of course, some are running Linux / Unix rather than Windows.

Hope that explanation helps.

BTW, with your Application Protocol, do you need to have a "start-of-statement" character if you're going to have an "end-of-statement" character ? If you think about how the data stream will arrive it might go something like
Code:
<This is a Statement><This is another Statement><This is yet another Statement>
where "<" is the start-of-statement character and ">" is the end-of statement character.

Every ">" , except for the very last, is followed by "<" which makes ">" redundant. You could just use an end-of-statement characer to imply the start of the next statement.

Just a thought.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data

Last edited by DougT; 08-17-2007 at 01:16 AM.
Reply With Quote
  #11  
Old 08-19-2007, 07:24 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

so , i've made the programs communicate with each other through a text code that i have encoded,

and i've made a simple fake game that sends info and commands back and forth,

i'm kinda confused as to what steps i need to take to make this an online server.
__________________
The Pho·net·ic Programmer
Reply With Quote
  #12  
Old 08-20-2007, 10:45 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

What bits aren't you clear about ?
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #13  
Old 08-21-2007, 07:48 AM
dilettante's Avatar
dilettanteneed to get started on sockets dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

As long as any software firewall on the hosting machine and any hardware firewall between the machine and the Internet allow connections in on the port you use you're set. Unless you're behind a NAT device, in which case you'll need to map that port to the machine on the private network.

Most likely you'll want a DNS name as well. If using an ISP doing dynamic address assignment you may want to look into a Dynamic DNS provider. Many are free for basic services.
Reply With Quote
  #14  
Old 08-21-2007, 01:49 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

sorry but i don't know anything that happens outside if VB.


so....


you're telling me that with my two experimental Winsock programs,
one to listen(game server) one to connect(client),

all i have to do is get a domain and i'll be up and running?


i don't really understand where the port and the IP adress gets checked for validation,
am i right to assume that the port and the IP is something that is set and checked by the company that provides the domain?



(i know my internet/computer has its own IP)
__________________
The Pho·net·ic Programmer
Reply With Quote
  #15  
Old 08-21-2007, 02:19 PM
dilettante's Avatar
dilettanteneed to get started on sockets dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

The IP address and port number are normally only "validated" upon connection by firewalls and network devices. Usually this "validation" is limited to whether to allow the connection or not. In some cases there may be whitelists or blacklists of connecting IP addresses or address ranges. Often these can be implemented at the server machine's OS level or in the service application itself.

In many simple cases it's just a yes/no for any connecting client IP though.

If your server machine is directly exposed to the Internet pretty much anyone can connect unless you implement something at the server. Exceptions may exist when your ISP blocks or proxies certain well-known ports, like those for SMTP mail or web servers.

An outside organization providing DNS services has nothing to do with access control. They just maintain the mapping between your server's IP address and its assigned DNS name in the global distributed directory that is DNS. You don't need a DNS assignment in order for clients to connect. It is a convenience, and if your IP address is dynamic it can be an important one.


But in the most general case there is no validation and anyone can connect to anyone else. That's how the Internet works. Everything else is just add-ons that may or may not be present in your case.

If you want user authentication and access control this must normally be added within your server application.

Don't overlook the NAT issue. Most consumers do not (and generally should not) directly expose machines' IP addresses to the Internet. Normally machines are given addresses from the non-routable private ranges, and rely on NAPT to provide port-by-port access by machines on the public Internet (most consumer so-called NAT devices are NAPT devices).

This requires port mapping to be set up at your NAPT router.
Reply With Quote
  #16  
Old 08-21-2007, 07:09 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

ok , so i guess my last question is,

what surprises can i look forward to if i try to set up a server that handles about 1000 people online at a time?
is there any other mandatory things i might need?


sorry for the dumb questions, but i want to know what i'm doing before i jump into a massive project or start spending money.
__________________
The Pho·net·ic Programmer
Reply With Quote
  #17  
Old 08-21-2007, 11:07 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

The obvious things like performance, responsiveness and network bandwidth limitations. Clearly, all these must be sufficient and be dependent upon the complexity of the Server's tasks and the amount of data traversing the Network.

The issues raised by Dilleante around security are important.

I don't know much about getting someone else to host the server as I don't and never have used anything other than my own machines (and am too mean to get a static IP Address) so I use the technique I described earlier to set-up a DNS entry and use that.

Saying that, I suppose that a 3rd party might put some constraints upon your usage but I believe that you can 'rent' a complete server to do with what you wish - at a price ! I'd do some research and discover what the offerings and costs are like. I've had a quick look and the prices seem to be quite high, to me but if you're intending that players 'subscribe' to your game somehow then you could recover some if not all of the costs - I just don't know as it's out of my experience.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #18  
Old 10-04-2007, 11:33 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

i have another question.

i got one on one communication to work but when i implemented the index and tried to make another client to be connected at the same time i get errors.

i set the index property to 100.

should this statement work or is this a Winsock bug?

Code:
Private Sub Form_Load()
Dim t As Integer

For t = 1 To 100
SockMain(t).LocalPort = txtPort.Text
SockMain(t).Listen
Next t


End Sub
__________________
The Pho·net·ic Programmer
Reply With Quote
  #19  
Old 10-04-2007, 11:55 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

No, it shouldn't work unless you have 100 winsocks on your form.

For multiple simultaneous connections it is normal practice to have one Winsock Listening and then to Load another and accept the connection request on that one
eg
Code:
Option Explicit

Dim intConnections As Integer


Private Sub Form_Load()
Winsock1(0).LocalPort = txtPort.Text
Winsock1(0).Listen
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
intConnections = intConnections + 1
Load Winsock1(intConnections)
Winsock1(intConnections).Accept requestID
End Sub
Each time a new client connects a new Winsock control array element is dynamically loaded for that client.(The above assumes you have a Winscok control - Winsock1 - drawn on your Form with an Index value of Zero)
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #20  
Old 10-05-2007, 12:02 AM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

so...by using your example...if i have 1000 users online, they all will go through "Winsock1" ?


sorry,kinda confusing to me
__________________
The Pho·net·ic Programmer
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
need to get started on sockets
need to get started on sockets
need to get started on sockets need to get started on sockets
need to get started on sockets
need to get started on sockets
need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets need to get started on sockets
need to get started on sockets
need to get started on sockets
 
need to get started on sockets
need to get started on sockets
 
-->