Trying to resolve error message

receiving error when client connects to server

Page 1 of 1

3 Replies - 1077 Views - Last Post: 14 July 2009 - 03:08 PM Rate Topic: -----

#1 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Trying to resolve error message

Post icon  Posted 14 July 2009 - 01:05 PM

Here is the error when I try to sign in. I hope someone can help me out!
[ERROR]
System.Net.Sockets.SocketException:No connection could be made because the target machine actively refused it 192.168.0.59:500 at system.Net.Sockets.TCPClient.Connect(String hostname, Int32 port) at WinClient.Form1.btnSignIn_Click(Object sender, EventArgs e)
[/ERROR]

Though I dont believe the issue is with the code here it is:

Imports System.Net.Sockets

Public Class Form1
	Const portNo As Integer = 500
	Dim client As TcpClient
	Dim data() As Byte

	Private Sub btnSend_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnSend.Click
		SendMessage(txtMessage.Text)
		txtMessage.Clear()
	End Sub

	Public Sub SendMessage(ByVal message As String)
		Try
			'---send a message to the server
			Dim ns As NetworkStream = client.GetStream
			Dim data As Byte() = _
			System.Text.Encoding.ASCII.GetBytes(message)
			'---send the text---
			ns.Write(data, 0, data.Length)
			ns.Flush()
		Catch ex As Exception
			MsgBox(ex.ToString)
		End Try
	End Sub

	Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
		Try
			Dim bytesRead As Integer
			bytesRead = client.GetStream.EndRead(ar)
			If bytesRead < 1 Then
				Exit Sub
			Else
				Dim para() As Object = _
				   {System.Text.Encoding.ASCII.GetString( _
				   data, 0, bytesRead)}
				Me.Invoke(New delUpdateHistory( _
				   AddressOf Me.UpdateHistory), para)
			End If
			client.GetStream.BeginRead( _
			   data, 0, CInt(client.ReceiveBufferSize), _
			   AddressOf ReceiveMessage, Nothing)
		Catch ex As Exception
		End Try
	End Sub

	Private Sub btnSignIn_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnSignIn.Click
		If btnSignIn.Text = "Sign In" Then
			Try
				'---connect to server
				client = New TcpClient
				client.Connect("192.168.0.59", portNo)
				ReDim data(client.ReceiveBufferSize)
				SendMessage(txtNick.Text)
				'---read from server
				client.GetStream.BeginRead( _
				   data, 0, CInt(client.ReceiveBufferSize), _
				   AddressOf ReceiveMessage, Nothing)
				btnSignIn.Text = "Sign Out"
				btnSend.Enabled = True
			Catch ex As Exception
				MsgBox(ex.ToString)
			End Try
		Else
			'---disconnect from server
			Disconnect()
			btnSignIn.Text = "Sign In"
			btnSend.Enabled = False
		End If
	End Sub

	Public Sub Disconnect()
		'---Disconnect from server
		Try
			client.GetStream.Close()
			client.Close()
		Catch ex As Exception
			MsgBox(ex.ToString)
		End Try
	End Sub

	'---delegate and subroutine to update the TextBox control
	Public Delegate Sub delUpdateHistory(ByVal str As String)
	Public Sub UpdateHistory(ByVal str As String)
		txtMessageHistory.AppendText(str)
	End Sub

	Private Sub Form1_FormClosing( _
	   ByVal sender As Object, _
	   ByVal e As System.Windows.Forms.FormClosingEventArgs) _
	   Handles Me.FormClosing
		Disconnect()
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to resolve error message

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Trying to resolve error message

Posted 14 July 2009 - 01:14 PM

I'm assuming you have a TCPListener on the computer your connecting to? Make sure the server is running before trying to connect, also make sure the TCPListener is listing on the same port your trying to send on
Was This Post Helpful? 0
  • +
  • -

#3 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Re: Trying to resolve error message

Posted 14 July 2009 - 01:20 PM

View PostPsychoCoder, on 14 Jul, 2009 - 12:14 PM, said:

I'm assuming you have a TCPListener on the computer your connecting to? Make sure the server is running before trying to connect, also make sure the TCPListener is listing on the same port your trying to send on


I am not sure if I have a TCPListener on the computer. How can I tell? Also how due I determine if the TCPListener is listening to the same port I am sending form?
Please continue to help!
Was This Post Helpful? 0
  • +
  • -

#4 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: Trying to resolve error message

Posted 14 July 2009 - 03:08 PM

Quote

I am not sure if I have a TCPListener on the computer. How can I tell? Also how due I determine if the TCPListener is listening to the same port I am sending form?
Please continue to help!


If you aren't sure, then you don't.
This computer: 192.168.0.59
Must have this port open: 500
In order for the connection to happen. The port must not be open, because that computer "actively refused" the connection.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1