Results 1 to 20 of 20

Thread: [2003] tcp client and server

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    [2003] tcp client and server

    I am tryin to make a simple server and client in vb.net that can connect through a port. I ahve looked around on the internet and have become completely confused. Any help is appreciated. I installed the vb6 socket control but i dont think that is the right way.

    Here is wat i have so far:

    Code:
    Public Class Form1
        Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(2999)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tcpserver.Server.Listen(5)
        End Sub
        Private Sub timeraccept_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeraccept.Tick
            tcpserver.AcceptSocket()
        End Sub
    End Class
    How do i make an event when a connect attempt is made?
    Last edited by qazwsx; Nov 4th, 2007 at 08:14 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2003] tcp client and server

    You should be getting a warning about the overload your using to create the TcpListener, you need to declare it like this instead:
    VB.Net Code:
    1. Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 50023)

    And I'd advise you to use a worker thread to handle all listening code. AcceptSocket is a blocking method so the main UI thread will basically stop responding until a connected socket has been returned.

    Here are some examples of mine.

    About your question, why do you want to raise an event if a connection has been made?
    Since AcceptSocket is a blocking method, a connection will always have been made when the execution reaches a line under AcceptSocket:

    VB.Net Code:
    1. Private Sub timeraccept_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeraccept.Tick
    2.         tcpserver.AcceptSocket()
    3.         MessageBox.Show("Since this line has been executed, a connection has been made!")
    4.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2003] tcp client and server

    You don't. As the documentation states, AcceptSocket and AcceptTcpClient block, which means that they will not return until they receive a connection request. I would suggest placing your call to AcceptSocket or, more likely, AcceptTcpClient, in a background thread. That way you can simply keep calling the method in a loop and, each time a connection request is received you can farm it out as required, either back to the UI thread or off onto a new thread of its own.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Thanks a lot for the help guys. I should be able to get it working now. I did not know what a blocking method was.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Ok I think i made the server correctly
    Code:
    Public Class Form1
        Dim tcpserver As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 2999)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tcpserver.Start()
            tcpaccept.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles tcpaccept.DoWork
            MsgBox("")
            tcpserver.AcceptTcpClient()
            MessageBox.Show("Since this line has been executed, a connection has been made!")
        End Sub
    End Class
    But when i try to make a client to connect to it, I get an error that the server did not respond in time. Here is the client
    Code:
    Public Class Form1
        Dim tcpclient As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient()
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            tcpclient.Connect(txtip.Text, 2999)
            Button1.Enabled = False
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            tcpclient.Close()
            Button1.Enabled = True
        End Sub
    End Class
    Can the client and server both be run from the same computer?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2003] tcp client and server

    Are the client and the server located on the same pc? are you sure you're using the right IP?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2003] tcp client and server

    What address are you trying to connect the client to?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Yes they are both on the same pc. Will this not work? I thought it was similar to how a portscanner can be run on the computer it is scanning. I am putting in my ip address in the client. Could my firewall be messing with the open port?
    I think it is the write ip. I got it from http://whatismyip.com/.

    Edit: nm i am an idiot and was not using the correct ip, the one from running ipconfig was different adn that one worked. Thanks for your patience.
    Last edited by qazwsx; Nov 5th, 2007 at 06:36 PM.

  9. #9
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    Hey,

    Well i did some things with the TCP server and clients.
    If there is an pending connection you can see that in TCPLISTNER.pending = true.

    Very easy to use a timer TICK every 0.1 sec that checks the pending state.

    so
    if pending = true then
    tcplistner.accept()
    end if

    this is the easy-est way to check incomming connections.

    But now you olso wanna do something with that connection.
    So you must store the accepted client someone like:

    my_tcp_client = tcpListner.accept()

    But after this with checking if there is data incomming things will get quite hard. You must do some threading to create data-arrival, dataSended, disconnected events.

    And after all you write your own wonderfull network program that will be quite simular to WinSock.

    So what i suggest is leave all the tcp-socket stuff and get the winsock.dll.
    It provides all you need. Including events and the multi-threading.

    I managed once to get a full working multi client program with that system.net.sockets and beleave me. . . if its only for your own use. Use winsock.

    Greets

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Ok i took your advice and got winsock, but i connot figure out how to get the server to accept an incoming connection. Here is what i have so far:

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            win.LocalPort = 2999
            win.RemotePort = 9999
            win.Listen()
        End Sub
    
        Private Sub win_ConnectionRequest1(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles win.ConnectionRequest
            MsgBox("1")
            win.Accept(sender)
            MsgBox("2")
        End Sub
    I get the first msgbox but never the second one.

  11. #11
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    its not sender you must get but

    win.accept(e.client)
    winsock just inherits system.net.sockets.
    So it will ask a socket to accept wich can be found in the event arguments(e).
    You can find the connecting port and ipadress 2.

    Greets

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Thx for the quick reply i think im on the right track now but:
    Code:
        Private Sub win_ConnectionRequest1(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles win.ConnectionRequest
            win.Accept(e.requestID)
            MsgBox("2")
        End Sub
    Still will not show the second message box.

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2003] tcp client and server

    Why would you want to add extra depencies to your application when everything needed is included in the .Net framework? The user have to install the .Net framework wether you'd like it or not, so not using what its providing is a waste. Besides, you're going to be able to get alot more help from the guys in the .Net section of VBforums if you stay with the standard classes.

    The reason why your client couldnt connect to the server is because you gave it the external IP, you have to make sure the needed ports are opened and the firewall properly configured.
    If you instead of using the external IP, would have used 127.0.0.1 or "localhost", it would've probably have worked better.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    True true is what you say atheist but just copy an .dll to your .exe location works fine.

    Btw i added the newest version(altrought it says its for vb.net 2005) i hope it works for 2003.

    I work with this one since it came out in april this year and it works fine.
    If you want to i can olso post the pure source of the winsock component and then you will se a simiular it is to what a lot of programmers want to write on their own.

    Greets
    Attached Files Attached Files

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    Ok this i what i ahve using your winsock:
    Code:
    Public Class Form1
    
        Private Sub btnmsgbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmsgbox.Click
            win2.Send("1")
        End Sub
    
        Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
            Dim ip = txtip
            MsgBox(win2.RemoteServer)
            win2.Close()
            win2.Connect(ip, 2999)
            btnconnect.Enabled = False
            btnmsgbox.Enabled = True
        End Sub
    
        Private Sub btndisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisconnect.Click
            win2.Close()
            btnconnect.Enabled = True
            btnmsgbox.Enabled = False
        End Sub
    
        Private Sub win_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent)
            MsgBox("ERROR")
        End Sub
    End Class
    Everything works fine now, except i cannot assign the ip of the remote server at runtime. If i try to i get an error that says no source code. Is there another way to assign the ip to connect to? If i set the variable "ip" to anyhting i get the same error.

  16. #16
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    you tried just using "Localhost" on the IP adress? And you dont need to close the win2 before connection.

    Greets

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    local host works, but wat would i do if i wanted the client to connect to a server on a different computer?

  18. #18
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    When connecting from one other pc to an other PC you should use your LAN ipaddress. But when you are connecting to your server that is not in your network you should set-up your server on a pc that is open for your port.
    When you wanna connect to your pc from the outside you must configure your modem/router to redirect that port you are using to that pc.

    Greets

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Re: [2003] tcp client and server

    The connecting works fine because all of the address are set to lacal host. But if i try to set the ip at runtime i get an error for no source code.

  20. #20
    Lively Member
    Join Date
    May 2007
    Posts
    87

    Re: [2003] tcp client and server

    That is probably because you cant change the ipaddress after you started listning or after you got connected. You have to close/dispose and =new winsock the control you are using to change the IP you are listning on /connecting 2.

    Tip. For testing winsock.send you should not try to send "1". Winsock sends bytes(). Sometimes(dont ask me why or how) it will have problems with sending numbers only. Try a normal string "sdfsdfsdgsdfgf" or something.

    Can you post/send you code so i can look @ it?

    Greets.

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