Results 1 to 18 of 18

Thread: [RESOLVED] problem with code - sockets

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Resolved [RESOLVED] problem with code - sockets

    hi guys
    i am going through some msdn sample packages.

    i am trying to connect to the tcpclient. ( chat program on sockets)

    this works fine if i use "localhost" but i want this to connect over the internet

    i am getting the error 'No such host is known'

    VB Code:
    1. Dim serverIP As IPAddress = ipaddress.Parse("X4.X5.XXX.36")
    2.  
    3.         client = New TcpClient("serverIP", PORT_NUM)

    if anyone can see any possible problems please let me know,
    it works 60% of the time, all the time.

  2. #2

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    so it looks correct then ?
    it works 60% of the time, all the time.

  3. #3
    Lively Member
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    68

    Re: problem with code - sockets

    I'd lose the quotation marks.

    client = New TcpClient(serverIP, PORT_NUM)

  4. #4

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    yeah now the target machine is actively refusing the connection!!!

    this is a nightmare lol
    it works 60% of the time, all the time.

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: problem with code - sockets

    If the server isn't listening for connections and accepting it's not your code that's the problem it's the server you're trying to use.
    Chris

  6. #6

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    yeah i think your right
    i hate this kind of thing.
    i know the code is probably most likely ok , but the settings on the pc' messes it up
    it works 60% of the time, all the time.

  7. #7
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: problem with code - sockets

    If your code works on local host as a server it's probably the remote server thats the problem, does it work on localhost?
    Chris

  8. #8

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    yeah it works on local host,
    it doesnt work when i use the ip address though.
    its really frying my brains lol
    it works 60% of the time, all the time.

  9. #9

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    is there anyone who could guide me through the steps to resolve this

    i am behind a router and i believe i have the port open

    An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

    Additional information: No connection could be made because the target machine actively refused it
    note client and server is on one machine

    any ideas would be much appreciated as i have been trying to get this working a long time
    it works 60% of the time, all the time.

  10. #10
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: problem with code - sockets

    If it works on local host but not with the router IP it must a problem with port forwarding. Check on your routers control panel that the port you use is being forwarded to the host which your running the server on.

    Also can you upload all of your code, as you seem to have not declared PORT_NUM.
    Last edited by the182guy; Feb 14th, 2007 at 08:35 AM.
    Chris

  11. #11

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    yeah i have declared PORT_NUM

    i will go in and recheck, the router but i am like 100% sure the port is open and forwarded. - obviously not though
    on the router my pc is addressed to
    10.0.0.20
    it works 60% of the time, all the time.

  12. #12

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    yes i have it set up as an inbound service

    service is called chat

    Details :
    Chat(TCP/UDP:1828)
    action - allow always
    send to lan server - 10.0.0.20

    note 1828 is the PORT_NUM i have declared in my code

    EDIT: when i check attached devices on the router
    10.0.0.20 is not there

    it has never raised an issue before though
    Last edited by d2005; Feb 14th, 2007 at 09:51 AM.
    it works 60% of the time, all the time.

  13. #13

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    client code

    VB Code:
    1. Const READ_BUFFER_SIZE As Integer = 255
    2.     Const PORT_NUM As Integer = 1828
    3.  
    4.     Private client As TcpClient
    5.     Private readBuffer(READ_BUFFER_SIZE) As Byte
    6.  
    7.  
    8.  
    9.     ' Pop up a Connect user dialog and send a message requesting user to log in to chat.
    10.     Sub AttemptLogin()
    11.         Dim frmConnectUser As New frmConnectUser()
    12.         frmConnectUser.StartPosition = FormStartPosition.CenterParent
    13.         frmConnectUser.ShowDialog(Me)
    14.         SendData("CONNECT|" & frmConnectUser.txtUserLogin.Text)
    15.         frmConnectUser.Dispose()
    16.     End Sub
    17.  
    18.     ' Clear the Users listbox, and request the server to send the list of users.
    19.     Private Sub btnListUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListUsers.Click
    20.         lstUsers.Items.Clear()
    21.         SendData("REQUESTUSERS")
    22.     End Sub
    23.  
    24.     ' Send the contents of the Send textbox if it isn't blank.
    25.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    26.         If txtSend.Text <> "" Then
    27.             DisplayText("You say: " & txtSend.Text & Chr(13) & Chr(10))
    28.             SendData("CHAT|" & txtSend.Text)
    29.             txtSend.Text = ""
    30.         End If
    31.  
    32.     End Sub
    33.  
    34.     ' Writes text to the output textbox.
    35.     Private Sub DisplayText(ByVal text As String)
    36.         txtDisplay.AppendText(text)
    37.     End Sub
    38.  
    39.     ' This is the callback function for TcpClient.GetStream.Begin to get an
    40.     ' asynchronous read.
    41.     Private Sub DoRead(ByVal ar As IAsyncResult)
    42.         Dim BytesRead As Integer
    43.         Dim strMessage As String
    44.  
    45.         Try
    46.             ' Finish asynchronous read into readBuffer and return number of bytes read.
    47.             BytesRead = client.GetStream.EndRead(ar)
    48.             If BytesRead < 1 Then
    49.                 ' If no bytes were read server has close.  Disable input window.
    50.                 MarkAsDisconnected()
    51.                 Exit Sub
    52.             End If
    53.  
    54.             ' Convert the byte array the message was saved into, minus two for the
    55.             ' Chr(13) and Chr(10)
    56.             strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
    57.  
    58.             ProcessCommands(strMessage)
    59.  
    60.             ' Start a new asynchronous read into readBuffer.
    61.             client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
    62.         Catch e As Exception
    63.             MarkAsDisconnected()
    64.         End Try
    65.     End Sub
    66.  
    67.     ' Send the server a disconnect message  
    68.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    69.         ' Send only if server is still running.
    70.         Try
    71.             If btnSend.Enabled = True Then
    72.                 SendData("DISCONNECT")
    73.             End If
    74.         Catch ex As Exception
    75.             MsgBox(ex.Message)
    76.         End Try
    77.      
    78.     End Sub
    79.  
    80.     ' When the form starts, this subroutine will connect to the server and attempt to
    81.     ' log in.
    82.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    83.         Dim frmConnectUser As New frmConnectUser()
    84.  
    85.         'Try
    86.         ' The TcpClient is a subclass of Socket, providing higher level
    87.         ' functionality like streaming.
    88.  
    89.         'Dim hostName As String = System.Net.Dns.GetHostName()
    90.         'Dim ipaddress As String = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString
    91.         'Dim serverIP As IPAddress = IPAddress.Parse("localhost")
    92.         'ipaddress = "84.45.225.63"
    93.         'client = New TcpClient("serverIP", PORT_NUM)
    94.         ' said local host
    95.  
    96.         'Dim serverIP As IPAddress = IPAddress.Parse("84.45.225.63")
    97.         'client = New TcpClient(New IPEndPoint(serverIP, PORT_NUM))
    98.  
    99.         Try
    100.             client = New TcpClient("84.45.225.63", PORT_NUM)
    101.             ' Start an asynchronous read invoking DoRead to avoid lagging the user
    102.             ' interface.
    103.             client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
    104.  
    105.             ' Make sure the window is showing before popping up connection dialog.
    106.             Me.Show()
    107.  
    108.             AttemptLogin()
    109.             'Catch Ex As Exception
    110.             '   MsgBox("Server is not active.  Please start server and try again.", _
    111.             '         MsgBoxStyle.Exclamation, Me.Text)
    112.             ' Me.Dispose()
    113.             'End Try
    114.  
    115.         Catch ex As Exception
    116.             MsgBox(ex.Message)
    117.         End Try
    118.    
    119.     End Sub
    120.  
    121.     ' This subroutine adds a list of users to listbox.
    122.     Private Sub ListUsers(ByVal users() As String)
    123.         Dim I As Integer
    124.         For I = 1 To users.Length - 1
    125.             lstUsers.Items.Add(users(I))
    126.         Next
    127.     End Sub
    128.  
    129.     ' When the server disconnects, prevent further chat messages from being sent.
    130.     Private Sub MarkAsDisconnected()
    131.         txtSend.ReadOnly = True
    132.         btnSend.Enabled = False
    133.     End Sub
    134.  
    135.     ' Process the command received from the server, and take appropriate action.
    136.     Private Sub ProcessCommands(ByVal strMessage As String)
    137.         Dim dataArray() As String
    138.  
    139.         ' Message parts are divided by "|"  Break the string into an array accordingly.
    140.         dataArray = strMessage.Split(Chr(124))
    141.  
    142.         ' dataArray(0) is the command.
    143.         Select Case dataArray(0)
    144.             Case "JOIN"
    145.                 ' Server acknowledged login.
    146.                 DisplayText("You have joined the chat" & Chr(13) & Chr(10))
    147.             Case "CHAT"
    148.                 ' Received chat message, display it.
    149.                 DisplayText(dataArray(1) & Chr(13) & Chr(10))
    150.             Case "REFUSE"
    151.                 ' Server refused login with this user name, try to log in with another.
    152.                 AttemptLogin()
    153.             Case "LISTUSERS"
    154.                 ' Server sent a list of users.
    155.                 ListUsers(dataArray)
    156.             Case "BROAD"
    157.                 ' Server sent a broadcast message
    158.                 DisplayText("ServerMessage: " & dataArray(1) & Chr(13) & Chr(10))
    159.         End Select
    160.     End Sub
    161.  
    162.     ' Use a StreamWriter to send a message to server.
    163.     Private Sub SendData(ByVal data As String)
    164.         Dim writer As New IO.StreamWriter(client.GetStream)
    165.         writer.Write(data & vbCr)
    166.         writer.Flush()
    167.     End Sub
    168. End Class
    it works 60% of the time, all the time.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: problem with code - sockets

    Change
    VB Code:
    1. client = New TcpClient("84.45.225.63", PORT_NUM)
    to
    VB Code:
    1. client = New TcpClient("10.0.0.20", PORT_NUM)
    and see if it works. If it does it's a router problem, if it doesn't it's a problem with your code.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  15. #15

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    well then its a router problem because i know that works

    what a noghtmare
    it works 60% of the time, all the time.

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: problem with code - sockets

    Is 84.45.225.63 your public IP? The public IP of the computer you're running this code on? If so, your router probably doesn't do loopback. The only solution, if you have to have it work this way, is to replace the router with one that does loopback.

    A router capable of loopback recognizes that the IP you're trying to reach is your own, so it just connects you to the internal address instead, 10.0.0.20 in this case. A router that doesn't do loopback looks on the WAN side for that IP and, since it can't see itself, it can't connect.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  17. #17

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: problem with code - sockets

    thanks Al42

    so in your opinion, i could continue to devlop using my 10.0.0.20 on my development machine.

    then when i move it out to deploy it (server on different fixd ip behind a different router). it should could work
    it works 60% of the time, all the time.

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: problem with code - sockets

    It won't work if you use the server's private IP from outside the LAN. You'll have to forward the port in the router to the server and access the public IP from outside. If the router supports loopback you can access it from inside the LAN by using the public IP also, but if it doesn't you have to use the private IP from the LAN.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width