Jump to content



Photo
vb.net winsock

  • Please log in to reply
28 replies to this topic

#16 OP togermano

togermano

    hot top for the bottoms

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

Posted 08 November 2012 - 23:10

never mind i got it


#17 Andre S.

Andre S.

    Asik

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

Posted 08 November 2012 - 23:16

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

Put that code in its own method and pass the method as a delegate to a BackgroundWorker. There are many more options for threading in .NET but that's probably the simplest one here.

#18 OP togermano

togermano

    hot top for the bottoms

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

Posted 09 November 2012 - 13:48

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.


Currently I got it working that when I write something in a telnet client and using IFs it will trigger a msgbox depending on what I wrote into the telnet client. You say ifs a terrible way to do it then what is a good way to do it?

#19 Andre S.

Andre S.

    Asik

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

Posted 09 November 2012 - 18:35

For instance, if your messages are text messages of the form "1,2" where 1 and 2 are the line and column to play at, you could put all your textboxes in a two-dimensional array, parse the message as two integers, and use these as indexes into the array to obtain the corresponding textbox. No need for any ifs, works with any board size.

#20 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 13:22

Thanks! I may try to do it the if way and then try it with your way after

I ran into another problem I can't access any of form1s command buttons from the tcp/ip thread I have tried using form1.label1.text = "connected!!" it doesnt error but it doesnt work either. I googled the problem but none of it was any help

http://stackoverflow...hread-in-vb-net

#21 Andre S.

Andre S.

    Asik

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

Posted 10 November 2012 - 17:30

You're not allowed to update the UI from any other thread than the UI thread. Use the Invoke (synchronous) or BeginInvoke (asynchronous) method of the main form to pass UI updates (as delegates) from a background thread.

#22 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 19:01

You're not allowed to update the UI from any other thread than the UI thread. Use the Invoke (synchronous) or BeginInvoke (asynchronous) method of the main form to pass UI updates (as delegates) from a background thread.


Is there a sample of that somewhere?

#23 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 19:20

Public Shared Sub ticserver()



server = Nothing
Try
CheckForIllegalCrossThreadCalls = False
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

server = New TcpListener(localAddr, port)

' Start listening for client requests.
server.Start()

' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing

' Enter the listening loop. While True
Console.Write("Waiting for a connection... ")

' Perform a blocking call to accept requests. ' You could also user server.AcceptSocket() here.
client = server.AcceptTcpClient()

Console.WriteLine("Connected!")
Form1.PictureBox1.Show()
Form1.Label1.Text = "A client has connected to you"
Form1.Label1.Show()
data = Nothing

' Get a stream object for reading and writing
stream = client.GetStream()

Dim i As Int32

' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine("Received: {0}", data)

' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)

' Send back a response.
stream.Write(msg, 0, msg.Length)

Console.WriteLine("Sent: {0}", data)

i = stream.Read(bytes, 0, bytes.Length)

End While

' Shutdown and end connection
' client.Close()

' Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try

Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
End Sub 'MainEnd Class'MyTcpListener

according to this http://tech.xster.ne...eads-on-vb-net/ adding the code

Me.CheckForIllegalCrossThreadCalls = False
should make it work but it doesnt..... why does me. always error I remove it and the error is gone but still this code does nothing

"
Form1.PictureBox1.Show()
Form1.Label1.Text = "A client has connected to you"
Form1.Label1.Show()
data = Nothing"


#24 Andre S.

Andre S.

    Asik

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

Posted 10 November 2012 - 19:49

Is there a sample of that somewhere?

Of course, googling any of the terms I mentionned will return you hundreds of useful results.

Don't disable checking for illegal cross-thread calls. You're making an illegal cross-thread call, disabling checking won't make it any less illegal.

#25 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 20:05

I dont get any of the codes they all use me.whatever and that always causes an error

#26 Andre S.

Andre S.

    Asik

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

Posted 10 November 2012 - 21:03

I dont get any of the codes they all use me.whatever and that always causes an error

"Me" is a reference to the current object, for instance if your code is inside your main Form class (like most tutorials assume), then "Me" is a reference to the Form object, which has an Invoke and a BeginInvoke method. Since I suppose you segregated your background thread code to another class, you'll have to pass it a reference to the Form object in order to invoke any methods of the Form from it. Alternatively, you could simply bring that code back to the main Form class and you'd be able to use "Me" to reference to Form then.

#27 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 21:50

I did I tried form1 and it didnt work even with the illegals off

#28 OP togermano

togermano

    hot top for the bottoms

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

Posted 10 November 2012 - 22:00

I got it working I had to change Public Shared Sub ticserver() too Public Function ticserver()

#29 OP togermano

togermano

    hot top for the bottoms

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

Posted 11 November 2012 - 00:50

I got the basis of it working..... Just gotta work on disabling the buttons until the next persons turn then reverse that for the other player :)

and somehow pick a random player to go first then alert both server and client of choice



Click here to login or here to register to remove this ad, it's free!