Jump to content



Photo
vb.net winsock

  • Please log in to reply
28 replies to this topic

#1 togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 25 October 2012 - 22:52

In class we made a simple tictactoe game in vb.net but I wanna try to go the extra mile and make it playable over the network.... I know alot about tcp/ip like I would need to pick one of the computers to host it with a port a number then one of the programs need to be the client and connect to that server.... I remember teaching myself on how to make a simple chat program in vb6 using the winsock ocx but how would I do it in vb.net?


#2 Eric

Eric

    Neowinian Senior

  • Tech Issues Solved: 11
  • Joined: 02-August 06
  • Location: Greenville, SC

Posted 25 October 2012 - 22:54

Check out the MSDN docs on TcpClient/TcpListener. They're quite useful. If you want something more full-featured that's already designed check out Lidgren.

#3 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 26 October 2012 - 11:51

http://msdn.microsof...istener.aspx#Y0 so how do I use this code? Where do I put it all? I obviously dont put it all in a command button....

#4 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 26 October 2012 - 23:04

No, you could start your TcpListener on the OnLoad() method of the main form. That said, if you're just starting out programming, adding TCP/IP multiplayer to your Tic-Tac-Toe is probably beyond your abilities at the moment. You'll need to define a message format, serialize/deserialize your messages through the connection, ensure synchronisation of all clients, handle disconnects and re-connects gracefully etc. It's not trivial.

#5 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 27 October 2012 - 10:37

No, you could start your TcpListener on the OnLoad() method of the main form. That said, if you're just starting out programming, adding TCP/IP multiplayer to your Tic-Tac-Toe is probably beyond your abilities at the moment. You'll need to define a message format, serialize/deserialize your messages through the connection, ensure synchronisation of all clients, handle disconnects and re-connects gracefully etc. It's not trivial.


copy and paste it onload and it dont work and the message part is easy just alot of ifs "if client sends "X:1"" Put an x mark on button 1 so on.....

#6 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 28 October 2012 - 01:28

copy and paste it onload and it dont work and the message part is easy just alot of ifs "if client sends "X:1"" Put an x mark on button 1 so on.....

How does it not work? What do you expect it to do? Do you have a client as well? A listener is just that, a listener, it listens to incoming connections. You need one instance of your app that acts as the server and one (or multiple others) that act as the client. There's a link to an example TcpClient in that same tutorial page for TcpListener. Read up on tcp connections, how to establish a connection, how to pass messages around, how to close the connection.

"A lot of ifs" is a terrible way of going about this btw. What would you do for a 10x10 tic-tac-toe? Code 100 if-elses? What if you wanted to support multiple different sizes? If you don't know about lists and loops I don't think there's any way you're going to add multiplayer functionality. Start one step at a time.

#7 Riva

Riva

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 11-February 07

Posted 28 October 2012 - 01:38

Use UDP and turn it to P2P. The server will be used only for discovery
This will reduce network utilisation

#8 Riva

Riva

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 11-February 07

Posted 28 October 2012 - 10:14

I wish I could remember VB.NET but it has been a decade...
The class to use is System.Net.Socket if you are aware of the Winsock OCX

#9 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 28 October 2012 - 20:56

How does it not work? What do you expect it to do? Do you have a client as well? A listener is just that, a listener, it listens to incoming connections. You need one instance of your app that acts as the server and one (or multiple others) that act as the client. There's a link to an example TcpClient in that same tutorial page for TcpListener. Read up on tcp connections, how to establish a connection, how to pass messages around, how to close the connection.

"A lot of ifs" is a terrible way of going about this btw. What would you do for a 10x10 tic-tac-toe? Code 100 if-elses? What if you wanted to support multiple different sizes? If you don't know about lists and loops I don't think there's any way you're going to add multiplayer functionality. Start one step at a time.


The code errors when I try to run it..... I am not stupid when it comes to this stuff i have used winsock before i Know i need a client and a server I took a ccna class all about tcp/ip

#10 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 28 October 2012 - 23:55

The code errors when I try to run it....

What's the error then?

#11 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 29 October 2012 - 11:29

What isnt an error?

Attached Images

  • Capture.JPG


#12 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 29 October 2012 - 14:12

and i dont care if my code is crappy i wanna do it to learn and fix it up later with better coding x

#13 Flatval

Flatval

    Neowinian

  • Joined: 09-February 11

Posted 29 October 2012 - 14:30

What isnt an error?


First off, move your "imports" lines above the Public Class Form1 - they need to stay first in the code document.

Then remove the class MyTcpListener, and the public shared main sub - they do not belong inside another Sub/Function/Method.

Next you will figure out that this code has an endless loop that will basicly lock up your program as soon as it starts (it will never stop "loading"). You need to either put this code in a separate thread or use the TcpListener asynchronously (spelling?)

Have a look at http://msdn.microsof...=vs.90).aspx#Y0 for examples

#14 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 29 October 2012 - 17:51

Your code doesn't compile. It's not valid Visual Basic and Visual Studio should show you some error messages in its output and error windows when you attempt to start the program. These error messages can often tell you what the problems are and how to fix them.

- The Imports directives should go at the top of the file, not inside a class declaration
- Your MyTcpListener class is declared inside the Form1 class, which is illegal - move it after the "End Class" of Form1 or, better, to its own file
- You're declaring a Main() function inside MyTcpListener, but your project is probably a Windows Forms Application, which already has its own Main() function - a .NET program must have only one Main() function.
- The code that goes inside that Main() method should go in the OnLoad() method of the Form1 class, as I explained to you before

Do you even know what is a class, a module, a namespace, what do access modifiers do, how to override a virtual method, what are events, how to register/unregister to them, what's a Shared method vs non-Shared, what is the purpose of the Main() method, etc? Implementing multiplayer will require putting together several pieces which you seem to ignore the very nature of. It's not like you're going to copy-paste the correct code here and there and it's all magically going to work; even if it did, that wouldn't be very useful to you, would it? Start by properly learning Visual Basic and the .NET Base Class Library and then you'll be able to implement advanced functionality like this and understand what you're doing. Learn to walk before you run...

#15 OP togermano

togermano

    hot top for the bottoms

  • Joined: 29-December 02
  • Location: MA,USA
  • OS: win98se(For real)

Posted 08 November 2012 - 22:13

thanks everyone and yes it is in an endless loop how do I put it in its own thread?