Ping and General Structure C# - Visual Studio 2010 - c#

Initially let me give an overview of my application. I am attempting to use Visual C# to do a PING on an address the user specifies. The user interacts with the system by entering the address they wish to PING into the textbox - the user then clicks the pingButton which will ping the desired address and then return the results to the user via a message box.
This is just the initial stage of the application.
I am having problem with the following code:
using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace Ping_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pingButton_Click(object sender, EventArgs e)
{
int timeOut = 300;
int ttl = 300;
string stat = "", data = "[012345678901234567890123456789]";
PingOptions pingOpts = new PingOptions();
pingOpts.Ttl = ttl;
pingOpts.DontFragment = true;
Ping pinger = new Ping();
PingReply reply = pinger.Send(pingAddressTextBox.Equals, timeOut, Buffer, pingOpts);
if (reply.Status.ToString() != "Success")
stat = "Failed";
else
stat = reply.Status.ToString();
pinger.Dispose();
MessageBox.Show("Congratulations!");
}
}
}
This code is from another topic on stack overflow - I am trying to gain an understanding by getting the code working and then modifying it.
The errors are:
Error 1 'System.Buffer' is a 'type' but is used like a 'variable'
Error 3 Argument 1: cannot convert from 'method group' to 'System.Net.IPAddress'
Error 4 Argument 3: cannot convert from 'System.Buffer'to 'byte[]'
Error 2 The best overloaded method match for 'System.Net.NetworkInformation.Ping.Send(System.Net.IPAddress, int, byte[], System.Net.NetworkInformation.PingOptions)' has some invalid
arguments
As I say I am just learning - this is just for a bit of a laugh - any help appreciated.

You need an object of type Buffer to pass to pinger.Send.
Here you just have the type name. You need an actual byte array:
Ping pingSender = new Ping ();
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
// Wait 10 seconds for a reply.
int timeout = 10000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);
// Send the request.
PingReply reply = pingSender.Send ("www.contoso.com", timeout, buffer, options);
Source
Here buffer is created from the string. You're missing this step.

Related

Send data to specific client from another client with a server in middle[C#]

I have searched everywhere but couldn't find as they are all answering to send message to all clients. What I want to achieve is multiple clients request to server to request data from another client and other client sends data to server telling it that data is for requesting client and so. I don't know how to achieve this. I'm new to this.
What I want to achieve:
I have tried with Data sending client to listen and requesting client to connect to it and transfer data. I have achieved this on local network but to make it work online it needs port forwarding and my user will be a lot of different people so port forwarding is not possible for every user. So I can rent a server which will act as a center of transfer. I programmed a test server in console which will listen to a server IP:port X and accept new clients and their data on port X and forward it to server IP:port Y but what this does is send data to all clients on port Y. I cannot send it to clients public ip address directly for obvious reasons. I understand that all the requesting clients are connected to port Y but I cannot create and assign new ports to all the clients interacting. So I want a way to determine how to request and receive the data without the need of assigning or creating new ports to different clients on same server.
What I have tried:
Server code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test___server
{
class server
{
public static string serverIP = "192.168.0.102";
static void Main(string[] args)
{
Thread listenSendingThread = new Thread(listenSending);
listenSendingThread.IsBackground = true;
listenSendingThread.Start();
Thread listenReceivingThread = new Thread(listenReceiving);
listenReceivingThread.IsBackground = true;
listenReceivingThread.Start();
Console.ReadKey();
}
public static List<TcpClient> listSending = new List<TcpClient>();
public static List<TcpClient> listReceiving = new List<TcpClient>();
public static TcpClient clientSending = null;
private static void listenSending()
{
TcpListener listenerSending = new TcpListener(IPAddress.Parse(serverIP), 5319);
listenerSending.Start();
Console.WriteLine("Server listening to " + serverIP + ":5319");
while(true)
{
clientSending = listenerSending.AcceptTcpClient();
listSending.Add(clientSending);
Console.WriteLine("Sender connection received from " + clientSending.Client.RemoteEndPoint);
}
}
private static void send()
{
StreamWriter sw = new StreamWriter(clientSending.GetStream());
sw.WriteLine(message);
sw.Flush();
Console.WriteLine("Message sent!");
}
public static string message = string.Empty;
private static void listenReceiving()
{
TcpListener listener = new TcpListener(IPAddress.Parse(serverIP), 0045);
listener.Start();
Console.WriteLine("Server listening to " + serverIP + ":0045");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
listReceiving.Add(client);
Console.WriteLine("Receiver connection received from " + client.Client.RemoteEndPoint);
StreamReader sr = new StreamReader(client.GetStream());
message = sr.ReadLine();
send();
}
}
}
}
Requesting client code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test____admin
{
class admin
{
static void Main(string[] args)
{
Console.WriteLine("Begin");
string serverIP = "192.168.0.102";
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect(serverIP, );
Console.WriteLine("Connected");
while (true)
{
Console.WriteLine("Reading");
StreamReader sr = new StreamReader(clientSocket.GetStream());
Console.WriteLine("Message: " + sr.ReadLine());
}
}
}
}
Request satisfying client code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Test___client
{
class client
{
public static string serverIP = "192.168.0.102";
static void Main(string[] args)
{
clientConnect();
}
private static void clientConnect()
{
try
{
TcpClient client = new TcpClient(serverIP, 0045);
StreamWriter sw = new StreamWriter(client.GetStream());
sw.WriteLine("Karan!");
sw.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
You are using a very low-level API, and doing it the right way is challenging. Instead, try YARP as a reverse proxy. The requesting client should notify the reverse proxy about the desired destination client. One option is sending the destination client name in the request header. You will also need to split a single server request into multiple client requests, then merge their responses into a single one. You can achieve it by implementing Transphorms.
I'm not sure this approach applies to your situation because clients should implement server API using REST, Grpc or any other supported technology.

Error avoidance when socket communication on unity

I am trying to receive socket communication with C # on Unity.
The following unityRecieve.cs will result in an error if Send.py is interrupted.
Send.py
import socket
import random
HOST = '127.0.0.1'
PORT = 50007
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
  a = random.randrange(3)
  result = str(a)
  print(a)
  client.sendto(result.encode('utf-8'),(HOST,PORT))
  time.sleep(2.0)
unityRecieve.cs
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class unityRecieve : MonoBehaviour
{
static UdpClient udp;
void Start()
{
int LOCA_LPORT = 50007;
udp = new UdpClient(LOCA_LPORT);
udp.Client.ReceiveTimeout = 100;
}
void Update()
{
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP);
string text = Encoding.UTF8.GetString(data);
Debug.Log(text);
}
}
https://jump1268.hatenablog.com/entry/2018/11/25/143459
How can I make unitiRecieve.cs keep running without giving an error message when Send.py is interrupted?
Not sure if you have considered exception handling. If you did not, this might point you in the right direction.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

How to check and ignore message from serial port?

i want to ignore all messages that came to serial port except unique. i add each message to hashSet and when new message arrive i check that this messages not contains in hashSet, if this message not contains i want to print him, right now my program think that each messages arrived is unique and i don't understand why my comparing code not working, maybe somebody can help me. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace mySniffer
{
class Program
{
static void Main(string[] args)
{
HashSet<String> messages = new HashSet<String>();
SerialPort comPort = new SerialPort();
comPort.BaudRate = 115200;
comPort.PortName = "COM4";
comPort.Open();
while (true)
{
string rx = comPort.ReadLine(); //reading com port
messages.Add(rx); // Add new incoming message to hashSet
if (!messages.Contains(rx))
{
Console.WriteLine(rx); // write incoming message
}
else {
Console.WriteLine(messages.Count); // check how many messages in hashSet
}
}
}
}
}
The problem was in logic of code, messages.Add(rx); should be moved in if block.

Trying to create a chat application in Visual Studio 2010

I am a beginner in networking but I have to start with something, so I decided to create a chat app in visual studio 2010 using C# language (winforms).
I googled a lot about that and I've found almost exactly what I needed.
I found the following code samples (in C# - console):
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=VS.71).aspx
I want to create that application using TCP protocol (I don't know if there are easier ways to do that, but I understood the basics of TCP when I tried to create that chat in C#.
When I executed the code samples in the links above, they worked! So I tried to adapt those samples at my chat application.
My chat application consists actually of two apps: a server and a client. Both of them have the same GUI (two text boxes, a button and two labels for displaying whether the client is connected to the server).
textBox1 on the server/client app is the one that display the message sent by the client/server app.
In the textBox2 on the server/client app the user types a message and then presses the button to send the message to the client/server app.
Let me show you what I've tried until now:
This is the server application code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Server_TCP_WINFORMS
{
public partial class Form1 : Form
{
//Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 13000);
public Form1()
{
InitializeComponent();
server.Start();
}
byte[] bytes = new byte[256];
String data = null;
TcpClient client = new TcpClient();
bool sw = true;
int data_at_byte_level = 0;
private void Form1_Load(object sender, EventArgs e)
{
try
{
label2.Text = "Waiting for an incoming connection...";
if (!server.Pending())
{
label2.Text = "For moment, there are no connections requests!";
}
else
{
client = server.AcceptTcpClient();
label2.Text = "Connected!";
sw = false;
}
}
catch (SocketException xyz)
{
MessageBox.Show("Exception occured!");
}
if (sw == false)
{
NetworkStream stream = client.GetStream();
while ((data_at_byte_level = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes);
textBox1.Text += data;
data = null;
bytes = null;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
String dataT;
if (textBox2.Text!=null && sw == false)
{
NetworkStream stream = client.GetStream();
dataT = textBox2.Text;
byte[] msg = System.Text.Encoding.ASCII.GetBytes(dataT);
stream.Write(msg, 0, msg.Length);
}
}
}
}
And this is the client application code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace Client_TCP_WINFORMS
{
public partial class Form1 : Form
{
TcpClient client = new TcpClient("127.0.0.1", 13000);
public Form1()
{
InitializeComponent();
label2.Text = "Conected to the server!";
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream stream = client.GetStream();
if (textBox2.Text != null)
{
String data_str = textBox2.Text;
Byte[] data_byte = System.Text.Encoding.ASCII.GetBytes(data_str);
stream.Write(data_byte, 0, data_byte.Length);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Byte[] data_byte = new Byte[290];
int bytes;
string Data;
NetworkStream stream = client.GetStream();
bytes = stream.Read(data_byte, 0, data_byte.Length);
Data = System.Text.Encoding.ASCII.GetString(data_byte, 0, bytes);
textBox1.Text += Data;
}
}
}
I want that those two apps to behave in the following way: I start the server application then I start the client application. When both of them are open, I want them to be already connected (because it's simpler that way, I think).
Then, I want that both of them to be receptive which means that when the server (for example) sends a message to the client, the latter one should receive the message and display it. If the server send another message, the client should receive and display it too.
If the user (of the client or the user of the server) presses the send button, the application should send the message from the textBox2 to the other application. How can I do those things in windows forms?
I see in the code samples in console that there is a main loop where the server reads the message from the client. But what if the server wants to send a message too? If the send button is pressed, the event for the button_pressed occurs and then it sends the message, but when it finished sending the message, it goes back to the main loop?
Please excuse my english. I am not a native speaker.
Thank you respectfully.
"When both of them are open, I want them to be already connected (because it's simpler that way, I think)."
For this, you need to use UDP(User Datagram Protocol) rather than TCP

SharpSVN connection rate limit to localhost?

We use SharpSVN to programmatically access SVN repositories. Now we have the problem that the access to local repositories via svn:// or http:// urls is very slow - every access needs at least one second, and our app needs to fetch a bunch of properties and directory listings.
We could reproduce the problem on two different machines, both are Windows 7 32 bit and are in the same domain. The SVN servers are VisualSVN 2.1.9 for http:// urls and the CollabNet 1.6.17 for svn:// urls. It appears for connections via "localhost" and via the host name. It appears in our C# application, as well as a small testbed app using IronPython and when calling the SharpSvn svn.exe command.
This problem does not happen when accessing when accessing remote repositories (Both a linux and a windows XP server) - here, each access is between 0.01 and 0.08 secs, which is expected due to network latency. The Problem also does not happen when acessing the local repositories via file:// urls or when accessing the repositories via "native" svn command line tools from CollabNet.
Now my question is: Has Windows 7 or .NET or SharpSVN some built-in limit which only applies to localhost connections?
(Addition: I now found out that this limit also applies when connecting via a small C# test program using System.Net.Sockets.TcpClient:
Server:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace TcpSpeedServer
{
class Program
{
public static void Main()
{
Int32 port = 47011;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
var server = new TcpListener(localAddr, port);
server.Start();
Console.WriteLine("Listening on {0} : {1}", localAddr, port);
ulong count = 0;
// Enter the listening loop.
while(true)
{
using (var client = server.AcceptTcpClient()) {
Console.WriteLine("Connected: {0} {1}!", count, client.Client.RemoteEndPoint);
count += 1;
using (var stream = client.GetStream()) {
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader = new StreamReader(stream))
{
string query = reader.ReadLine();
writer.WriteLine("GET / HTTP/1.0");
writer.WriteLine();
writer.Flush();
}
}
}
}
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace TcpSpeedTest
{
class Program
{
const bool ASYNC = false;
static DateTime s_now;
public static void Main(string[] args)
{
var liste = new List<object>();
s_now = DateTime.Now;
for (int i=0; i < 100; i += 1) {
if (ASYNC)
ThreadPool.QueueUserWorkItem(connect, i);
else
connect(i);
}
Console.WriteLine("outer: " + (DateTime.Now - s_now));
Console.ReadLine();
}
private static void connect(object i)
{
DateTime now = DateTime.Now;
using (TcpClient client = new TcpClient("localhost", 47011))
{
var stream = client.GetStream();
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader = new StreamReader(stream))
{
writer.WriteLine("GET / HTTP/1.0");
writer.WriteLine();
writer.Flush();
string result = reader.ReadLine();
}
}
Console.WriteLine(string.Format("inner: {0} - {1} - {2}", i, DateTime.Now - now, DateTime.Now - s_now));
}
}
}
So this problem seems not to be subversion specific.)
Addition2: When running the client under Mono 2.10 for windows, the problem does not appear. So it seems to be specific to .NET framework.
Addition3: It seems to be an IPv6 related problem. The server only listens on IPv4, but the hostname also resolves to IPv6. Now it seems that the OS code internally tries the IPv6 connection, and after getting the connection reset, waits 1 sec before falling back to IPv4. And this game is repeated for every single connection attempt. http://msdn.microsoft.com/en-us/library/115ytk56.aspx documents that for TcpClient (thanks to Andreas Johansson from the MSDN forums for the hint!), and it seems that the APR used by Apache internally uses a similar mechanism.
Addition 3 is also the solution to your problem. To fix this, either make DNS/hosts file only resolve to an IPv4 address, or make the IPv6 server(s) work.
You can enter in C:\Windows\System32\drivers\etc\hosts something like:
127.0.0.1 localhost-ipv4
And then use that name to connect.
You can also make svnserve listen to IPv6 addresses. A quick search for svnserve options [revealed][1] that it defaults to IPv6, so in its startup parameters is probably a --listen-host. Try removing that, or when it's not present forcing it to run at IPv6.
The same can be done for the Apache webserver:
Listen 0.0.0.0:80
Listen [::]:80

Categories

Resources