Here is a sample of my code. It doesn't work. I sent acknowledgement in the right form {0x01} but the device always returns only IMEI. Could someone solve this problem?
static void Main(string[] args)
{
TcpListener list = new TcpListener(new IPEndPoint(IPAddress.Any, 2065));
TcpClient client;
Console.WriteLine("Listening... \n");
list.Start(1);
list.Server.NoDelay = true;
while (true)
{
Console.WriteLine("Waiting for client...\n");
client = list.AcceptTcpClient();
Console.WriteLine("Client connected ");
byte[] imei = new byte[8192];
NetworkStream ns = client.GetStream();
if (ns.CanRead)
{
ns.Read(imei, 0, (int)client.ReceiveBufferSize);
}
Console.WriteLine(Encoding.ASCII.GetString(imei, 0, imei.Length));
byte[] ack = new byte[1] {0x01};
if (ns.CanWrite)
{
ns.Write(ack, 0, ack.Length);
}
client.Close();
}
}
Like Ron said, you keep sending the same thing over and over. After you receive IMEI, your code sends correct response and disconnects. So next connection starts from beginning, with IMEI... The data from device comes over the same connection, so... why disconnect? ;)
– mlask
Related
I am trying to send a message from my laptop to my desktop over the LAN.
I have written a Tcp server that listens for a connection on a port, and a client that tries to connect to it and send a simple string as a message.
Server:
void Listen()
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
while (true)
{
Debug.Log($"listening to {listener.LocalEndpoint}...");
var socket = listener.AcceptSocket();
Debug.Log("connected!");
var bytes = new byte[100];
int k = socket.Receive(bytes);
var data = Encoding.ASCII.GetString(bytes, 0, k);
Debug.Log(data);
socket.Send(new byte[] {1});
Debug.Log("responded");
socket.Close();
Debug.Log("closed connection");
}
}
Client:
public void Send()
{
client = new TcpClient();
Debug.Log("connecting...");
client.Connect("192.168.0.12", port);
Debug.Log("connected!");
var data = Encoding.ASCII.GetBytes("Hello!");
var stream = client.GetStream();
Debug.Log("sending data");
stream.Write(data, 0, data.Length);
Debug.Log("data sent! waiting for response...");
var reply = new byte[100];
stream.Read(reply,0,100);
Debug.Log("received response");
client.Close();
}
They communicate correctly when they are both on the same machine, but the client never connects to the server when I am running it on a different machine.
I have a rule in my firewall that allows an incoming connection through the port I have specified.
I have tried it with both computer's firewalls off and the connection still doesn't go through.
Any help would be greatly appreciated.
I have a "Sysmex XP-300" machine who sends data via TCP, I need to catch this data and reply with ASCII, to verify that the it was received.
The problem is, that every time the machine is connected, the code will reply "connected" non stop. I also need to reply with ASCII that my machine seems not receive. Here my code:
class Program
{
static string ack = char.ConvertFromUtf32(6);
byte[] byData = System.Text.Encoding.ASCII.GetBytes(ack);
const int PORT_NO = 3001;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
TcpServer tcpServer = new TcpServer(SERVER_IP, 3001);
tcpServer.Connected += new TcpServer.TcpServerEventDlgt(OnConnected);
tcpServer.StartListening();
Console.WriteLine("Press ENTER to exit the server.");
Console.ReadLine();
tcpServer.StopListening();
}
static void OnConnected(object sender, TcpServerEventArgs e)
{
// Handle the connection here by starting your own worker thread
// that handles communicating with the client.
Console.WriteLine("Connected.");
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(ack);// ASCIIEncoding.ASCII.GetBytes(textToSend);
//---send the text---
Console.WriteLine("Sending : " );
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
}
}
I wrote the code as seen below as a C# console app, and it works correctly.
However when I use in win application it dose not work.
I put in a break point, and it stops on " tcpClient = tcpListener.AcceptTcpClient();" line,
and does not do any work and does not show any thing, and no errors are returned.
part of my code :
void _func_send()
{
byte[] bytes = new byte[512];
IPAddress add = ipHost.AddressList[2];
Socket server = new Socket(add.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
TcpListener tcpListener = null;
tcpListener = new TcpListener(add, 50);
//tcpListener.Stop();
tcpListener.Start();
while (true)
{
/////on this line it stops
tcpClient = tcpListener.AcceptTcpClient();
stream = tcpClient.GetStream();
if (stream.DataAvailable)
{
stream.Read(bytes, 0, bytes.Length);
_txt_show.Text = _txt_show.Text + Encoding.UTF8.GetString(bytes);
byte[] msg = Encoding.UTF8.GetBytes(".:: thanks ::.");
stream.Write(msg, 0, msg.Length);
}
}
}
My IP-Camera is configured to send alarm messages to my computer.
IPAddress of camera: 10.251.51.1
IPAddress of my computer: 10.251.51.136
The camera can be configured to send messages to any port on my computer.
So, I set the alarm destination to my computer's IP Address: 10.251.51.136 at port 3487.
Now, what should be done at my computer to get the message sent by the camera ?
I tried to write a TCPListener from MSDN and the code is below:
Int32 port = 3487;
IPAddress localAddr = IPAddress.Parse("10.251.51.136");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
server.Start();
// Start listening for client requests.
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
server.Start();
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
But this does not seem to be working.
Or is my entire approach flawed ? Should i implement TCPListener or TCPServer instead for my scenario above?
Any pointers why?
I have a client application that serializes a object and sends it to a server application. The server should deserialize the object, make changes to it, then serialize it and send it back.
Server Code:
TcpClient client = server.AcceptTcpClient();
using(NetworkStream stream = client.GetStream())
{
using(StreamReader streamReader = new StreamReader(stream))
{
string xmlData = streamReader.ReadToEnd();
}
}
The ReadToEnd doesn't return unless the client closes the stream. But if the client closes the stream, I can't send a response.
Is there a better way to do this?
You can signal "end of data" by closing only your half of the duplex TCP connection. This is accomplished with Socket.Disconnect.
See how it works with this example, which I kept similar to yours. The client sends the data and then calls Disconnect; this allows ReadToEnd to return while still keeping the server's half of the connection open. The server then sends a response and also disconnects, after which both parties can Close their end of the connection to tear it down.
static void Main(string[] args)
{
Action clientCode = () =>
{
var buffer = new byte[100];
var clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(IPAddress.Loopback, 6690);
clientSocket.Send(buffer);
clientSocket.Disconnect(false);
Console.WriteLine("Client: message sent and socket disconnected.");
while (true) {
var bytesRead = clientSocket.Receive(buffer);
if (bytesRead == 0) {
break;
}
Console.WriteLine("Client: read " + bytesRead + " bytes.");
}
clientSocket.Dispose();
};
var server = new TcpListener(IPAddress.Loopback, 6690);
var thread = new Thread(new ThreadStart(clientCode));
server.Start();
thread.Start();
var client = server.AcceptTcpClient();
using(NetworkStream stream = client.GetStream()) {
using(StreamReader streamReader = new StreamReader(stream))
{
var data = streamReader.ReadToEnd();
Console.WriteLine("Server: read " + data.Length + " bytes.");
// Since we 're here we know that the client has disconnected.
// Send the response before StreamReader is disposed, because
// that will cause the socket itself to be closed as well!
Thread.Sleep(TimeSpan.FromSeconds(1));
Console.WriteLine("Server: sending response.");
stream.Write(new byte[10], 0, 10);
Console.WriteLine("Server: closing socket.");
}
}
server.Stop();
Console.WriteLine("Server: waiting for client thread to complete.");
thread.Join();
return;
}
You could use a higher level framework like WCF, or if you are hell-bent on managing your own streams, then don't use ReadToEnd()- use ReadLine() (and have the client send messages as lines), or use Read() and have a special character (a sentinel) represent the end of a message.