Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have developed a Sub that receives data over a TCP Port, The data gets send from a RTU Device. My code works fine i only have one problem and can seem to get it sorted out and any help will really be appreciated :)

Problem : My server will work fine for a few hours and then it will stop receiving the data, if i close the application and open it again then it receives the data again. It almost as if the port stops responding and the i need to restart it.

Here is my sample code can u please check it for me and advise what can be my problem and if u have sample code for me
VB
    Private Sub StartProcess()
    '// SET THE LISTENER TO LISTEN ON PORT
    Dim serverSocket As New TcpListener(4050)
    '// DeCLARE VERIBALS
    Dim iCounter As Integer
    '// START THE LISTENER FOR SERVER
    serverSocket.Start()
    '// WRITE MESSAGE TO UI
    '// WRITE MESSAGE TO UI
    Console.WriteLine(" ")
    Console.WriteLine("***** Server Started on Port:4050 *****")
    Console.WriteLine(" ")

    iCounter = 1
    '// START INFINITE LOOP
    For iCounter = 1 To 2
        iCounter = 1
        Try

            '// DECLARE NETWORK STREAM
            Dim mySocket As Socket = serverSocket.AcceptSocket()
            NetworkStream = New NetworkStream(mySocket)
            '// SET THE BYTE LENGTH
            Dim lngByte As Long = 29
            Dim bytesFrom(lngByte) As Byte
            '// READ AND WRITE BYTES FROM NETWORK STREAM
            NetworkStream.Read(bytesFrom, 0, lngByte)
            NetworkStream.Write(bytesFrom, 0, lngByte)
            '// CONVERT BYTES TO STRING
            dataFromClient = BitConverter.ToString(bytesFrom)
            '// CLOSE SOCKET
            mySocket.Close()
        Catch ex As Exception
        End Try
    Next
    serverSocket.Stop()
    ' msg("exit")
    'Console.ReadLine()
End Sub
Posted
Updated 31-Jan-11 20:18pm
v2

1 solution

First, if this is TCP, why not using a bit higher-level TcpListener class? Do you always have one client or more? With TcpListener you can permanently listen to new connections on one thread and work with client (or clients) on another. On client side you could use TcpClient. Just the idea, very robust.

By the way, where is the code for other side (if it does not hang, it does not mean we don't need its code)?

Now, let me tell you: you already made a big crime: you block your exception from propagation in the cycle. You should never ever do it. Move exception block on the very top of each thread: this is the only place where you can block propagation of exception. And -- make a full dump of exception (include stack and all inner exceptions, recursively) and place a dump in system event log. When you reproduce a problem, read the logs. If you still cannot figure out your problem, post follow-up question here (please don't do "Add answer" do "Improve question" or "Add comment".

—SA
   
v2
Comments
Sandeep Mewara 1-Feb-11 1:50am
   
Looks like OP totally ignored your last sentence :)
comment from OP:
Thanks For the response Smile

I will Look into using the "TcpListener" we have multipule clients connecting the max is 255 clients connecting to one port. I do not have the code for the onther side it is sent with a hardware device that we purchase from 3de party company and i communicate ModebusTCP Protocol with the device.
   
Ah, Modbus, sure, I know. So Modbus acts as a server, right? You know, most of the "industrial" programmers don't know how to write software. Now you have to ram with the company, don't hope for quick resolution. Who is the manufacturer (may be I know them)?

Anyway, my advice about dealing with exceptions still works and important. And try to dump exception anyway, you might have it on client side, too ("broken connection", blah-blah...), it may give you a clue.

Good luck, your follow-up questions are welcome! (Please don't post as an answer.)
--SA
Ryno Engelbrecht 9-Feb-11 4:42am
   
the mod-bus device is the client and my server is listening on the port for new messages. the problem i am having is for some reason my port on my server is falling asleep or not responding. can it be that i have to many connections to one TCP port?

I wrote a small simulator client application to see if the clients can connect to my server and on the client side i am getting a "Remote IP is not reachable" error message


Can u maby give me some demo code or snips to help me getting the server working correct?

my email addres is - rynoeengelbrecht at webmail dot co dot za

Thanks for all your help

God Bless!
   
Sorry, I don't think I have anything to help you here (also, I do not use VB. and don't advise it). It looks like you Modbus controller has some inherent problems and cannot handle situation correctly (it happens all the time). But... wait a minute! Maybe, this is also your mistake.

My only essential advice is: don't close remote socket! Only close it by the end of you application.
You effectively break the session from your side. Modbus detects closed socket, tries to fix it by re-creating session again and again and finally get screwed. Do a simple experiment: remove mySocket.Close() and see if it all works longer. Please report back if it works or not.
--SA
   
I knew... :-)
Sandeep Mewara 1-Feb-11 1:50am
   
BTW, good answer! :) 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900