Connecting two PCs through TCP using C# - c#

I'm trying to establish a TCP connection between two PCs (mine & friend's). Now, I know that the logic is very simple but I want to check if the connection is successful.
The friend of mine starts the server and gives me the IP address via whatismyip.com, but when I enter that IP I get the timeout error.
I'm not quite sure that this is the right way of establishing the connection to PC in another LAN. Will be glad for any help \ advises \ external links.
Below is my code:
internal class Server
{
private TcpListener m_server;
private readonly int m_port = 8888;
public void Start()
{
try
{
m_server = new TcpListener(IPAddress.Any, m_port);
m_server.Start();
Console.WriteLine("Server started");
while(true)
{
TcpClient client = m_server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] data = new byte[256];
int bytes = stream.Read(data, 0, data.Length);
string message = Encoding.UTF8.GetString(data, 0, bytes);
Console.WriteLine("From client: " + message);
stream.Close();
client.Close();
if (message == "Shutdown")
{
m_server.Stop();
}
}
}
catch(Exception e)
{
Console.WriteLine("Server: " + e.Message);
}
finally
{
m_server?.Stop();
}
}
}
internal class Client
{
private System.Net.Sockets.TcpClient m_client;
private readonly string m_ip = "";
private readonly int m_port = 8888;
private readonly string m_message = "Shutdown1";
public void Start()
{
try
{
m_client = new System.Net.Sockets.TcpClient();
m_client.Connect(m_ip, m_port);
NetworkStream stream = m_client.GetStream();
byte[] data = Encoding.UTF8.GetBytes(m_message);
stream.Write(data, 0, data.Length);
stream.Close();
m_client.Close();
Console.WriteLine("Done");
Console.ReadKey();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
}
}

Related

Unable to read data from tcp server

I have created a simple C# client application. Once it connects to the server it should read the messages sent from the server. It also has the ability to send messages to server too. However I am unable to figure out to correct way to read the data.
I am spawning a thread once it connects to the server. The thread runs in infinite loop and have two interfaces each for reading and writing. Connect() method is called from a ButtonClick event.
My code snippet is as below:
namespace WpfApp1
{
public class TCPClientClass
{
private StreamWriter SwSender;
NetworkStream Sender;
NetworkStream Receiver;
//private StreamReader SrReciever;
private Thread thrMessaging;
TcpClient tcp;
bool connected = false;
public bool Connected { get { return connected; } set { connected = value; } }
//public bool Connect(IPAddress IP, int nPortNo)
public async Task Connect(IPAddress IP, int nPortNo)
{
tcp = new TcpClient();
try
{
//tcp.Connect(strIPAddress.Parse("192.168.137.1"), 2000);
// tcp.Connect(IP , nPortNo);
await tcp.ConnectAsync(IP, nPortNo);
thrMessaging = new Thread(new ThreadStart(ThreadFunction));
thrMessaging.Start();
Connected = true;
}
catch
{
MessageBox.Show("Unable to connect to server");
//return false;
}
//return true;
}
public void Disconnect()
{
Sender?.Close();
Receiver?.Close();
tcp?.Close();
//tcp?.Client.Disconnect(false);
thrMessaging.Abort();
Connected = false;
}
private void ThreadFunction()
{
while (thrMessaging.IsAlive)
DoTasks();
}
private void DoTasks()
{
if (Connected)
{
var a = ReadMessages();
SendMessages();
}
}
private /*void*/async Task ReadMessages()
{
byte[] data = new byte[4096];
//Int32 bytesRead = 0;
//Task<int> bytesReadTask;
String responseData = String.Empty;
Receiver = tcp.GetStream();
try
{
//bytesReadTask = Receiver.ReadAsync(data, 0, data.Length);
//responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytesReadTask.Result);
var response = await Receiver.ReadAsync(data, 0, data.Length);
MessageBox.Show("Server response was " + response);
Thread.Sleep(1000);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void SendMessages()
{
try
{
string strSendData = "Hello from GUI";
Byte[] data = System.Text.Encoding.ASCII.GetBytes(strSendData);
Sender = tcp.GetStream();
Sender.Write(data, 0, data.Length);
Sender.Flush();
Thread.Sleep(1000);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
you should change
var response = await Receiver.ReadAsync(data, 0, data.Length);
MessageBox.Show("Server response was " + response);
to
var response = await Receiver.ReadAsync(data, 0, data.Length);
string result = System.Text.Encoding.Default.GetString(data);
MessageBox.Show("Server response was " + result);
if you´re still having problems..my server Code:
public class tcpServer
{
public void method()
{
TcpListener server = new TcpListener(IPAddress.Any, 9999);
server.Start();
TcpClient client = server.AcceptTcpClient();
NetworkStream ns = client.GetStream();
byte[] hello = new byte[100];
hello = Encoding.Default.GetBytes("hello world");
while (client.Connected)
{
ns.Write(hello, 0, hello.Length);
}
}
}

TcpListener Client stays connected sending multiple messages but server only receives or processes the first message

I am setting up a server to read some network clients using TcpListener. The Client sends some data I verify that data and send a response to that data, the client stays connected and sends a second response and I verify that data and send a response back, its like logging in to the server twice. The first login get sent back to the client just fine but the second time the client responds the server does not show that it received anymore data from the client.
I have tested it by setting up a dummy client (the real client is Cell phone based ODB2). With the dummy client set up I did verify that the first handshake happens but the when the client sends the second set of text it does not show up on the server.
class Program
{
static private TcpListener listener = null;
static private TcpClient client = null;
static private NetworkStream stream = null;
static private int iCount = 0;
static Int32 port = 8090;
static IPAddress localAddr = IPAddress.Parse("192.168.1.17");
static void Main(string[] args)
{
listener = new TcpListener(localAddr, port);
listener.Start();
while (true)
{
try
{
client = listener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(ThreadProc, client);
}
catch (IOException ioex)
{
RestartStream();
}
}
}
private static void ThreadProc(object obj)
{
var client = (TcpClient)obj;
Byte[] bytes = new Byte[client.ReceiveBufferSize];
stream = client.GetStream();
try
{
int bytesRead = stream.Read(bytes, 0, (int)client.ReceiveBufferSize);
string returndata = Encoding.ASCII.GetString(bytes, 0, bytesRead).Replace("-", "");
byte[] sendBytes;
if (returndata.ToLower().StartsWith("7e") && returndata.ToLower().EndsWith("7e"))
{
//… do stuff with the data and send it back to the client
sendBytes = Encoding.Default.GetBytes(login1);
stream.Write(sendBytes, 0, sendBytes.Length);
stream.Flush();
}
else
{
SaveStream(returndata);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Test Client Code:
//---data to send to the server---
string textToSend = "7E010000360141850000080000000000000000000000000000000000000000000000000000000000000035303030303038003131313131313131313131313131313131F67E";
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//---send the text---
Console.WriteLine("Sending : " + textToSend);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
string Text2 = "7E0100003601418535303030303038003131313131313131313131313131313131F67E";
Console.WriteLine("Sending : " + Text2);
byte[] bytesToSend2 = ASCIIEncoding.ASCII.GetBytes(Text2);
nwStream.Write(bytesToSend2, 0, bytesToSend2.Length);
client.Close();
Console.ReadLine();
What I need to happen is my understanding is the client stays connected the whole time and send data over and over, my system seems to accept it once and then stops receiving it, I need it to continue to receive the client data and process it.
Ok so I figured it out, there should be a second while loop in side the thread.
static void Main(string[] args)
{
listener = new TcpListener(localAddr, port);
var clientSocket = default(TcpClient);
listener.Start();
var counter = 0;
while (true)
{
clientSocket = listener.AcceptTcpClient();
var client = new ConnectedDevice();
client.startClient(clientSocket, counter.ToString(), sqlConnString);
}
}
ConnectedDevice class:
class ConnectedDevice
{
private TcpClient _clientSocket;
private string _clientNumber;
private string _sqlConnString;
public void startClient(TcpClient clientSocket, string clientNumber, string sqlConnString)
{
_clientSocket = clientSocket;
_clientNumber = clientNumber;
_sqlConnString = sqlConnString;
var ctThread = new Thread(ProcessClient);
ctThread.Start();
}
private void ProcessClient()
{
while (_clientSocket.Connected)
{
try
{
Byte[] bytes = new Byte[_clientSocket.ReceiveBufferSize];
var networkStream = _clientSocket.GetStream();
networkStream.ReadTimeout = 10000;
int i;
while ((i = networkStream.Read(bytes, 0, bytes.Length)) != 0)
{
var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i).Replace("-", "");
byte[] sendBytes;
Console.WriteLine(data);
string sLogin1 = "7E81000013014185000008000000000054523230313731303138303930303137497E";
sendBytes = Encoding.ASCII.GetBytes(sLogin1);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
}
catch (Exception ex)
{
}
}
}
}

TCP- I can't send data when listening as a server

I can't send data when I listen over TCP.I need to listen all the time incoming connections and I should send some data whenever needed.My server code starts to listen when program loads,but when I try to send data it gives this error:
"Only one usage of each socket address (protocol/network address/port) is normally permitted (typically under load)."
And here is my code:
public void Send(object sender, System.EventArgs e)
{
String str = "08";
String str2 = "4213";
String CRCvalue = CRC("08" + str2);
String str3 = str2 + CRCvalue;
Connect("localhost", "hello localhost " + Guid.NewGuid());
}
static void Connect(String server, String message)
{
try
{
Int32 port = 2101;
TcpClient client = new TcpClient(server, port);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
System.Diagnostics.Debug.WriteLine("Sent");
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
System.Diagnostics.Debug.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
System.Diagnostics.Debug.WriteLine("SocketException: {0}", e);
}}
protected override void OnLoad(EventArgs e)
{
CreateServer();
}
private static void CreateServer()
{
var tcp = new TcpListener(System.Net.IPAddress.Any,25565);
tcp.Start();
var listeningThread = new Thread(() =>
{
while (true)
{
var tcpClient = tcp.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(param =>
{
NetworkStream stream = tcpClient.GetStream();
string incomming;
byte[] bytes = new byte[1024];
int i = stream.Read(bytes, 0, bytes.Length);
incomming = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
System.Diagnostics.Debug.WriteLine(incomming);
tcpClient.Close();
}, null);
}
});
Any help will be apprecitated.
The exception tells you that you are trying to open the same server socket more than once. You should close your TcpListener (and join the listening thread) on form close. You could also use a singleton service, which manages the tcp listener (or assign a static field and only create the listener if the field is not set).

Socket Programming: Server/Clients and Thread Usage

static void Main(string[] args)
{
Console.Title = "Socket Server";
Console.WriteLine("Listening for client messages");
Socket serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress serverIp = IPAddress.Any;
IPEndPoint serverEP = new IPEndPoint(serverIp, 8000);
SocketPermission socketPermission = new SocketPermission(NetworkAccess.Accept,
TransportType.Tcp,
"127.0.0.1", 8000);
serverSocket.Bind(serverEP);
serverSocket.Listen(2);
while(true)
{
//Socket connection = serverSocket.Accept();
connection = serverSocket.Accept();
Thread clientThread = new Thread(new ParameterizedThreadStart(MultiUser));
clientThread.Start(connection);
}
}
public static void MultiUser(object connection)
{
byte[] serverBuffer = new byte[10025];
string message = string.Empty;
int bytes = ((Socket)connection).Receive(serverBuffer, serverBuffer.Length, 0);
message += Encoding.ASCII.GetString(serverBuffer, 0, bytes);
Console.WriteLine(message);
TcpClient client = new TcpClient();
client.Client = ((Socket)connection);
IntPtr handle = client.Client.Handle;
}
I want to write a chat program which has one server and 2 clients. The problem is that, I can not direct the message sent from the client1 to client2 via the server. How can the server distinguish threads so that it can send the received message from client1 to client2?
Each client has their own handle. You can access this via the Handle property. For example:
TcpClient client = tcpListener.AcceptTcpClient();
IntPtr handle = client.Client.Handle; //returns a handle to the connection
Then all you need to do is store this in a hashtable, and iterate through it, looking for available data. When you detect data on the wire for one of the connections, then save it and retransmit it to the other clients in the table.
Remember to make sure that you make this multithreaded so a listen request on one client does not block any send or receive functions on other clients!
I've added some code here you should be able to work with (tested it out on my system)
private void HandleClients(object newClient)
{
//check to see if we are adding a new client, or just iterating through existing clients
if (newClient != null)
{
TcpClient newTcpClient = (TcpClient)newClient;
//add this client to our list
clientList.Add(newTcpClient.Client.Handle, newTcpClient);
Console.WriteLine("Adding handle: " + newTcpClient.Client.Handle); //for debugging
}
//iterate through existing clients to see if there is any data on the wire
foreach (TcpClient tc in clientList.Values)
{
if (tc.Available > 0)
{
int dataSize = tc.Available;
Console.WriteLine("Received data from: " + tc.Client.Handle); //for debugging
string text = GetNetworkString(tc.GetStream());
//and transmit it to everyone else
foreach (TcpClient otherClient in clientList.Values)
{
if (tc.Client.Handle != otherClient.Client.Handle)
{
Send(otherClient.GetStream(), text);
}
}
}
}
}
public void Send(NetworkStream ns, string data)
{
try
{
byte[] bdata = GetBytes(data, Encoding.ASCII);
ns.Write(bdata, 0, bdata.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
protected string GetNetworkString(NetworkStream ns)
{
if (ns.CanRead)
{
string receivedString;
byte[] b = GetNetworkData(ns);
receivedString = System.Text.Encoding.UTF8.GetString(b);
log.Info("Received string: " + receivedString);
return receivedString;
}
else
return null;
}
protected byte[] GetNetworkData(NetworkStream ns)
{
if (ns.CanRead)
{
log.Debug("Data detected on wire...");
byte[] b;
byte[] myReadBuffer = new byte[1024];
MemoryStream ms = new MemoryStream();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length);
ms.Write(myReadBuffer, 0, numberOfBytesRead);
}
while (ns.DataAvailable);
//and get the full message
b = new byte[(int)ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(b, 0, (int)ms.Length);
ms.Close();
return b;
}
else
return null;
}
You will want to call HandleClients from a main thread that checks to see if there are any pending requests or not, and runs on a loop.

Data issue connecting from Flex (XMLSocket) to .NET (Socket)

I'm working on a chat/IM system for my game (which is written in Flex) and wanted to connect it to my server (which is written in C#) via sockets.
So, I've successfully connected them together using XMLSocket on Flex, and Socket on the server side, the client gets a connected event, sends data correctly, but when I try to send back data from the server to the client, nothing happens (even though the BeginSend callback shows that the entire buffer was sent).
Client:
private var hostName:String = "localhost";
private var port:uint = 4444;
private var socket:XMLSocket;
public var app:DotNETServerTest;
public function DotNetSocketExample() {
socket = new XMLSocket();
configureListeners(socket);
socket.connect(hostName, port);
}
public function send(data:Object):void {
socket.send(data);
}
public function disconnect():void {
socket.close();
closeHandler(null);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CLOSE, closeHandler);
dispatcher.addEventListener(Event.CONNECT, connectHandler);
dispatcher.addEventListener(DataEvent.DATA, dataHandler);
}
private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
app.send_btn.enabled = false;
app.disconnect_btn.enabled = false;
}
private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
app.send_btn.enabled = true;
app.disconnect_btn.enabled = true;
}
private function dataHandler(event:DataEvent):void {
trace("dataHandler: " + event);
Alert.show(event.data);
}
Server (Only specific parts):
// This is the call back function, which will be invoked when a client is connected
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
SocketClient NewConnection = new SocketClient(SocketServer.EndAccept(asyn));
// This is a test message I'm attempting to send
SendMessageTo(NewConnection, "<test></test>");
Clients.Add(NewConnection);
WaitForData(NewConnection);
LogMessage(NewConnection, "Client # {0} connected", Clients.Count);
SocketServer.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (ObjectDisposedException)
{
LogMessage("OnClientConnection: Socket has been closed.");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
internal static void SendMessageTo(SocketClient client, string Data)
{
byte[] byteData = Encoding.ASCII.GetBytes(Data);
SendMessageTo(client, byteData);
}
internal static void SendMessageTo(SocketClient client, byte[] byteData)
{
try
{
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
}
catch (Exception e)
{
LogMessage(client, "Error sending data to client: {0}", e.Message);
}
}
internal static void SendCallback(IAsyncResult ar)
{
// Retrieve the socket from the async state object.
SocketClient handler = (SocketClient)ar.AsyncState;
try
{
int bytesSent = handler.socket.EndSend(ar);
}
catch (Exception e)
{
LogMessage(handler, e.Message);
}
}
Please help!
Thanks, Ron
Found the problem - I didn't send out \0 at the end of each send.
Changed this:
byte[] byteData = Encoding.ASCII.GetBytes(Data);
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
To this:
byte[] byteData = Encoding.ASCII.GetBytes(Data + "\0");
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);

Categories

Resources