I want to listen specific port in c#, but I don't want to write a chat program in net.
I just want listen to a port and receive all of the bytes that come from that port.
I asked this question before but I did't get a useful answer. I say again, I don't want to have a client and server program, I want to just have a single program that run on my Computer and show me what bytes are received from specific port, or a program that show me what IP is connected to each port , like "netstat" command in CMD.(I don't want to use CMD command in my C# program)
please help me.
I think this should get you started. This will show you similar information to netstat:
using System;
using System.Net;
using System.Net.NetworkInformation;
static void Main()
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnections = ipGlobalProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation tcpConnection in tcpConnections)
{
Console.WriteLine("Local Address {0}:{1}\nForeign Address {2}:{3}\nState {4}",
tcpConnection.LocalEndPoint.Address,
tcpConnection.LocalEndPoint.Port,
tcpConnection.RemoteEndPoint.Address,
tcpConnection.RemoteEndPoint.Port,
tcpConnection.State);
}
}
To listen to a port, the sample code provided by Microsoft here should get you going.
You need a sniffer. Check Wireshark out.
Related
I try to write to a NdisProt driver and send raw ethernet packets. I imported some C++ comands to my C# program, so that I can access the driver. When I try to open a handle to the driver I always get a invalid handle. I already tried it with just "NdisProt" as the path, but it didn't solve it. Do you have any suggestions why i get a invalid handle?
private bool OpenDriver()
{
// User the CreateFile API to open a handle to the file
this.m_iHandle = CreateFile("\\\\.\\NdisProt,
GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// Check to see if we got a valid handle
if ((int)m_iHandle <= 0)
{
// If not, then return false and reset the handle to 0
this.m_iHandle = IntPtr.Zero;
return false;
}
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
Thanks for any help!
Update: I just solved the problem by adding another manifest, so that the application is run as admin.
The solution with the NDIS Driver did still not work so I searched for another solution. I found the SharpPcap library. With that library I am able to modify the packets I want to send e.g. change the Destination-MAC-Adress.
Thanks for your answers!
If you now any other solutions to send raw ethernet packets in a C#
program please let me know.
What about Socket class?
Constructor:
public Socket (System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType),
where socketType can take the next value: SocketType.Raw
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
You can import functions from Windows Winsock library and use SOCK_RAW flag when creating a socket.
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
So, i'm new to C# and visual studio and stuff. But i searched arround for how to get all ip's, Like you see in the Windows Firewall app. But I can't find any thing to get started with. I saw alot of posts about Dns, And stuff So i got this:
private void ip()
{
StringBuilder sb = new StringBuilder();
// Get host name
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
sb.AppendLine(strHostName + " - " + ipaddress);
label1.Text = sb.ToString().Replace(Environment.NewLine, "\n");
}
}
But I only get 4 ip's but i want all ip's currently connecting to my pc.
(And an i know there are alot of posts about this, but i'm searching for an answer for an beginning programmer to understand)
Could anybody help me?
Thanks!
That code you show gets the IPs assigned to the current machine.
What it sounds like you want is the equivalent of running netstat on the command line (get all active connections). This question should get you started: How to determine tcp port used by Windows process in C#
Basically you use GetActiveTcpConnections.
For a project i have to communicate with a netduino,
So i use serial communication to communicate with the netduino.
But here's my problem
I cannot find my Usb portname, i use this small piece of code to find the port names.
private void GetPortNames()
{
string[] ports = SerialPort.GetPortNames();
ComportListbox.DataSource = ports;
}
It doesnt show the usb port names.
What am i doeing wrong, or how can i fix this issue.
EDIT
Question edited:
Can i see the usbportname from my usbport where the NETduino is attached to. So i hope to see COM10 for example. I looked in the system managment and saw that the usb is called Port_#0001.Hub_#0001. How can i open this port.
If ComportListbox has an "add" method, why not just use that with a for loop.
foreach ( string portName in ports )
{
ComportListbox.Items.Add( portName );
}
If not, let me know and I will delete this answer.
Otherwise you may have to use a BindingList<string>. see: Binding List<T> to DataGridView in WinForm
Or you might even have to create an object that contains a string property for the binding name.
First I'm using Visual Studio 2012 with C# and the Pcap.Net Library.
I try to to forward packets which I captured before.
What I try to do:
Spoof ARP-Table of my phone.
Redirect the traffic which normally goes to the gateway to my computer.
Log the packets.
Forward them to the gateway.
What I did:
Spoofing ARP-Table -> works fine.
Redirect traffic to my PC -> works fine (logically).
Log the packets to a dumpfile (.pcap) as shown in the tutorial on this site -> works fine (I can open it and read it with wireshark and it looks good).
Forward the packets to the gateway. -> does not work.
I would like to forward them fluently. So what I did was use the "sendBuffer()" function as shown in the tutorial. So I just read in the .pcap file where all the packet information is saved and try to resend it with this "sendBuffer()" function. And of course I use the same adapter to do it.
When I capture the traffic with wireshark I can see that my packets don't even get sent.
(I'm also not sure if it works at the same time while I capture the data to the file. Because the code which should forward them need to read the packets from the file. Isn't there another way?)
My code to forward the packets from the .pcap file (the IDE doesn't give me any error):
It's approximately my code, I don't have it available cause I'm not at home. But should be right.
IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine;
PacketDevice selectedOutputDevice = devices[0];
long capLength = new FileInfo(#"E:\CSharp\Pcap\dumpFile.pcap").Length;
bool isSync = true;
OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(#"E:\CSharp\Pcap\dumpFile.pcap");
using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
if (inputCommunicator.DataLink != outputCommunicator.DataLink)
{
tB_Log.Text = tB_Log.Text + Environement.NewLine + "ERROR: Different Datalinks!";
}
using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
{
Packet packet;
while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
sendBuffer.Enqueue(packet);
}
outputCommunicator.Transmit(sendBuffer, isSync);
}
}
}
Thank you very much for helping!
I just setup memcached on a ubuntu system i have on my network. Didn't change any options so everything is default. I think tried to connect to the server using Enyim. It doesn't fail but when i try to retrieve the items they are always null. I'm not much of a low level won't but i've been able to discern things from wireshark before so i decided to give it a try. I'haven't been able to discern anything but i noticed the first .Store() command i sent actually sent network packets to the correct address. Every .Store() command did absolutely nothing.
Here is my app.config:
I've tried both "Binary" & "Text" Protocols and they did the same thing.
Here is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
namespace MemCacheTest
{
internal class Program
{
private static void Main(string[] args)
{
//var config = new MemcachedClientConfiguration();
//config.Servers.Add(new IPEndPoint(new IPAddress(new byte[] {10, 0, 0, 1}), 11211));
//config.Protocol = MemcachedProtocol.Binary;
var mc = new MemcachedClient();
for (var i = 0; i < 100; i++)
mc.Store(StoreMode.Set, "Hello", "World");
mc.Store(StoreMode.Set, "MyKey", "Hello World");
Console.WriteLine(mc.Get("MyKey"));
Console.WriteLine("It should have failed!!!");
Console.ReadLine();
}
}
}
Does anyone know whats going on or how i could determine what is wrong? I thought it was strange that i wasn't getting any exceptions so i set an invalid ip address in the config file. Same results.
The short answer: If your service is running check your port, it should be blocked somehow. (did you add an exception for port 11211 in Ubuntu firewall(iptables)?)
telnet 10.0.0.1 11211 If you have a telnet client on your server this will show the port is inaccessible.
Enyim doesn't throw you an error even the port is inaccessible. yes, it's strange.
To add to dasun answer. When you install memcached by default its configured to only listen on the "lo/localhost" interface. Its the only security memcache really has. Even if you try to telnet locally and do specify the lo interface for example:
telnet 10.0.0.1 11211
it will fail. To fix you have to go into the memcached.conf file and comment out
# -l 127.0.0.1
and then restart the service