Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Net / Sockets

Net / Sockets Components for network and related use

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2009, 3:09 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jun 2009
Age: 19
Posts: 6
Reputation: 0
coldfire417 is on a distinguished programming path ahead
Question Visual Basic 2008 Winsock Chat

Hey, I was wondering how I could actually make a WORKING chat system in visual basic 2008. I've watched tutorials on YouTube, and all of them fail and I get a weird error. I wan't one were you simply put in your buddie's IP and they put in yours and you can talk. A attachment or download link or project sample would be nice ...or code



Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-20-2009, 9:14 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Learn about TcpClient and TcpListener. To connect something need to be listening at the other end, a server, that is the job of the TcpListener. To start one create an instance where you specify which ip and port to listen from and call the Start method, keep it in a class variable. This is then needed at minimum at server:
Code:
Private server As TcpListener
Code:
server = New TcpListener(ip, port)
server.Start()
The server ip is the local IP address you wish to bind to, here's an example retrieving it:
Code:
Dim localIP As Net.IPAddress = Net.Dns.GetHostAddresses("")(0)
With a Timer you can check Pending property to see if a client is attempting to connect, then AcceptTcpClient to establish the connection:
Code:
If server.Pending Then
    client = server.AcceptTcpClient
The 'client' you can also keep in a class variable, it's functionality is same as described below for communication.
Code:
Private client As TcpClient
At client you can connect to the server with a TcpClient:
Code:
Private client As TcpClient
Code:
client = New TcpClient(ip, port)
For basic communication sending strings it is most convenient using StreamWriter/StreamReader, these can read/write via the NetworkStream of the TcpClient, keep them in class variables:
Code:
Private reader As IO.StreamReader
Private writer As IO.StreamWriter
initialize them after connection is made:
Code:
reader = New IO.StreamReader(client.GetStream)
writer = New IO.StreamWriter(client.GetStream)
writer.AutoFlush = True
Now you can write messages with a call to WriteLine method:
Code:
writer.WriteLine("message")
and read with ReadLine method if data is available, use a Timer to check this at intervals:
Code:
If client.GetStream.DataAvailable Then
    Dim msg As String = reader.ReadLine
The server and client functionality can be built into each client, so each client has the ability to receive connections. The server can also be a dedicated application that all clients connect to.

Finally I will mention cleanups, the server listener is closed with a call to Stop method:
Code:
server.Stop()
The sockets is closed by closing the NetworkStream of TcpClient
Code:
client.GetStream.Close()
Since this example uses stream-reader/writer you can also close the socket by closing/disposing the reader/writer:
Code:
reader.Dispose()
writer.Dispose()
The TcpClient is released by calling Close method:
Code:
client.Close()
Hope this brief introduction to sockets help.

You will also need to use Try-Catch with several of these calls, as they may and will throw exceptions. Reading the documentation and trying out things will help you understand this.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 6:07 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.