sniff the data on a selected port - C# - c#

I have one application running on specific port. I dont have access to this application but i know what it does. Now this application is listening on one specific port and process the data coming on that port
Now i have been assigned the task on logging all the packets received on that port with all the data details. I have used the wireshark and can apply the filter to check the data coming on that specific port. So i assume here that i have been stuck in creating this kind of snipping program so i can get all the data packet details. I have search the stack overflow and come up with
Code project
Stack overflow
Now i have evalute the sample and can see that it gives me packets but it listens on all port of the system. Not on the specific port. Can someone help me to achieve my solution? Basically if i will listen to all port then there are lots of app/program running on server so it may get bottleneck. Thank you all.

In MJsniffer.MJsnifferForm class you have ParseData method - at first step it converts received bytes to IPHeader object, an then there a huge SWITCH-CASE on ProtocolType field. Inside that SWITCH there are TCPHeader or UDPHeader is created and added to TreeView on form - there you can filter packages by SourcePort/DestinationPort fields tcpHeader/udpHeader.
Also if you are already familiar with WireShark, then you can easly switch to Pcap.NET wrapper(they use same packet capturing library - WinPcap)

Related

c# Sockets messages between client and server

I'm currently learning how to use sockets on c#, and have a question regarding how the messages should be between the client and the server.
Currently i have a server application and a client application, and in each application i have some strings that are the commands. When, for example, the client needs the time from the server, i have a string like this:
public const string GET_TIME_COMMAND = "<GET_TIME_COMMAND>";
Then i have a if statement on the server, thats checks if the message sent from the client starts with that string and if so, it sends another message to the client with another command and with the time in a json string.
My question is, is this a good way to do it, and if not could you advise me on another way to go about this?
TCP
Keep in mind that TCP is a stream based connection. You may or may not get the complete command in one message. You may even get multiple commands in one read.
To solve this TCP messages usually have a unique start and stop sequence or byte that may not be part of the message.
(SomeCommand)
Where ( is the start and ) is the stop symbol.
An alternative way os to prepend a header to the actual message that contains the message length.
11 S O M E M E S S A G E
Where 11 is the message length and somemessage is the actual message. You'd usually transmit the length as a byte or ushort, not a string literal.
In both cases you have to read over and over until you have one complete message - then you can dispatch it into the application.
Also TCP is connection based. You have to connect to the remote site. The advantage is that TCP makes sure that all messages are sent in the very order you put them in. TCP will also automatically re-send lost packets and you don't have to worry about that.
UDP
In contrast to that UDP is a message/packet based, but it is not reliable. You may or may not get the message and have to re-send it in some cases. Also UDP doesn't have a notion of a "session". You would have to do that yourself if required.
The answer to your question depends on the protocol used. For TCP this won't work well with your current message format. You'd probably have to prepend a header.
You could use UDP, but then you may have to detect and re-send messages that got lost.

C# struggling with sockets and trying to controll a hardware device op tcp/ip

I'm new to c# and try to control a device over tcp/ip. The device behaves as the server so what i have done so far is that my client program initiates the connection and successfully connects to the device.
But that's all i have. Once the connection is established the device starts sending all these hex strings that i somehow need to parse. I have the protocol manual for the device so i know how to read them but i have absolutely no idea how i would implement this in c#.
What i think that needs to be done is that i constantly need to listen for the device to send data.
Then i somehow need to find complete hex command strings. I know every string starts with "0x10, 0x02" and ends with the hex chars "0x10, 0x03".
The device dictates that i also need to send some sort of acknowledge message every time i receive a string. "0x10, 0x06" (ack) So there has to be some sort of two way communication.
Any help or guidance would be very much appreciated.
Thanks
You've got to keep the incoming data in a buffer and keep looking for that end character. When you find both start and end characters, you can run your custom "parse" method to extract the information and clear the buffer.
Here's a good starting point:
https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-client-socket-example
In the server part you can find an example of looking for the end character:
https://learn.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example

c# Socket send receive network

I'm totally confused and stunned about my problem. I wrote a multi client server using sockets.
I created a first GUI that receives clients and display them in a gridview, in the click gridview event it opens a new GUI with the socket between the clicked client and the server. So everything about the connection between server and the clients go well and fast
But my problems are :
the send information between them like sending full process it sometimes shown full and it sometimes less result .
send files management for example when the server request full folders/files in a directory sometimes it shows them all and sometimes less result .
But the command such open a window open an url, send a message , commands like those works so perfectly and instantly .
I am so confused right now what's the problem
Notice 1 : I used the extern IP for connection between server/client.
Notice 2 : internet connection is perfect (definitely not slow)
I tried to use different buffer sizes, but what I'm really confused about is that sometimes the result comes full with a specific buffer size and sometimes not with the same buffer size .
Thank you for your time !
I would also recommend you use wireshark so that you are able to get the full information and transactions that are really going on in the network.
This will help you to rule out where the issue is coming from.
To determine the packet size see the image
Here I am not talking about how the TCP handshaking protocol works as I assume it is working. However do note that in the transmission if you see something like RST then maybe somebody issued a reset command implying that some error occurred in the transmission. Such as something due to checksum for example. Although it is generally a problem for someone working directly on TCP/IP protocol
This should help you confirm that you sent and recieved the correct length.

confused when receiving data from serial port

I encountered a problem that took me too much time, but without resolving it.soI really want you to help me.
I have an application built with c # wpf, and communicates with ovens via serial port.
the frame I need to send have following form: [EOT] (GID) (UID) (Temp) [ENQ]
gid uid: group identifier and unit identifier (address of the machine).
(eof),(enq) :frames the message.
(temp) means: give me the temperature value.
the only machine that has the same address can answer (master slave architecture).
the form of the response message is: [STX] (Temp) <DATA> [ETX].
the field contain only the temperature value
stx start text. etx end text.
I have no problem with sending and receiving of data, and I can display the value of temperature for a single machine connected.
but when I connect More machines, I do not know which machine has answered the frame that I sent, because the response frame does not have any adress so that I can determine which oven have respond.
So the situation in brief is:
-I send Data to ovens.
- I received data.
- I can not decide which oven answered.
please any one have an idea.
PS: I work with the protocol:EI-BISYNCH of eurotherm EuroTherm
If needed: EI-Bisynch ASCII Sequence Diagrams
In these conditions, the typical solution is:
Send the request to the current device
Wait for an answer for a defined timeout
If we receive an an answer within the timeout, the device responded.
If we do not receive an answer, the device is offline, mark it as such.
Switch to the next device, goto 1
Basically you should be able to wrap into a loop the code described here:
Providing Asynchronous Serial Port Communication
That is a sample that works with an AutoResetEvent. One of the .Net multithreading that allows synchronizing threads (the threads that sends the request in the loop, and the threads that receive the message in the loop)
IN these situations, the machine that you addressed is responding (or at least its assumed to be) Single Master - Multi Slaves. Meaning :-
Master -> Hey #1 tell me your temp -> #1 SIR! YES SIR! 23 degrees!
Master -> Hey #2...
The idea is no other slave will respond. By convention of the protocol.
Its pretty hard to do anything but this kind of system on serial.
In terms of Design if you create something like a command queue. Each command knows what device it wants to talk to, and what question it wants to ask. You process each command, send the serial message, get the response, and give it back to the command. Now you have a command, which knows which device it talked to, and what the response of that device was.
As long as you only have one "in-flight" command to which you are waiting for a response, and you know which device you sent the command to, you can assume that the device that responds next is the device you asked to respond. Now this won't necessarily always be true if it is possible for your device to send un-prompted responses.

Asynch Socket Server and AS3 Flash Application - User Disconnects not handled

I have 2 sets of code for you to look at, both are available at PasteBin here:
First is my c# Socket server: http://pastebin.com/wvT4f19m
Second is my code within my AS3 application: http://pastebin.com/bKvabFSP
In the code, what I am trying to do is a simple Send/Receive to see what happens. If I open my application in 2 instances the c# socket server registers that they exist and all is fine!. If I close one of my instances, the c# server still thinks that the user exists and the socket isn't closed.
My code is based off the example at : http://msdn.microsoft.com/en-us/library/fx6588te.aspx
In the MS example, the following lines are added to the SendCallBack() function:
handler.Shutdown(SocketShutdown.Both);
handler.Close();
These definately close the sockets, something I do not want to happen.
I am new at socket programming and it has taken me a fair amount of time to play with the MS example to get it working roughly how I need it. The only problem is the acknowledgement of user disconnects so that I can remove the user from the Clients list that I have set up in the server. Also, when disconnects are acknowledged, I can inform other clients.
thanks all!
Upon each attempt to send data to the user, I do a quick check for a successful transmission/Poll the user and if it fails, the user is removed from my server.
Closing the socket won't affect your listener, it will only affect the current connection. Why do you say this is not what you want?
It sounds like this is exactly what you want.

Categories

Resources