i'm trying to write simple tcp\ip client-server.
here is server code:
internal class Program
{
private const int _localPort = 7777;
private static void Main(string[] args)
{
TcpListener Listener;
Socket ClientSock;
string data;
byte[] cldata = new byte[1024];
Listener = new TcpListener(_localPort);
Listener.Start();
Console.WriteLine("Waiting connections [" + Convert.ToString(_localPort) + "]...");
try
{
ClientSock = Listener.AcceptSocket();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
int i = 0;
if (ClientSock.Connected)
{
while (true)
{
try
{
i = ClientSock.Receive(cldata);
}
catch
{
}
try
{
if (i > 0)
{
data = Encoding.ASCII.GetString(cldata).Trim();
ClientSock.Send(cldata);
}
}
catch
{
ClientSock.Close();
Listener.Stop();
Console.WriteLine(
"Server closing. Reason: client offline. Type EXIT to quit the application.");
}
}
}
}
}
And here is client code:
void Main()
{
string data; // Юзерская дата
byte[] remdata ={ };
TcpClient Client = new TcpClient();
string ip = "127.0.0.1";
int port = 7777;
Console.WriteLine("\r\nConnecting to server...");
try
{
Client.Connect(ip, port);
}
catch
{
Console.WriteLine("Cannot connect to remote host!");
return;
}
Console.Write("done\r\nTo end, type 'END'");
Socket Sock = Client.Client;
while (true)
{
Console.Write("\r\n>");
data = Console.ReadLine();
if (data == "END")
break;
Sock.Send(Encoding.ASCII.GetBytes(data));
Sock.Receive(remdata);
Console.Write("\r\n<" + Encoding.ASCII.GetString(remdata));
}
Sock.Close();
Client.Close();
}
When i'm sending to my server i cannt receive data back answer. Sock.Receive(remdata) returns nothing! Why?
You're trying to receive to an empty buffer. You should allocate the buffer with a sensible size, and then take note of the amount of data received:
byte[] buffer = new byte[1024];
...
int bytesReceived = socket.Receive(buffer);
string text = Encoding.ASCII.GetString(buffer, 0, bytesReceived);
(It's somewhat unconventional to use PascalCase for local variables, by the way. I'd also urge you not to just catch Exception blindly, and not to swallow exceptions without logging them.)
Related
I am trying to create a async socket server but it does not wait for a client to connect and exits
class Program
{
IPAddress ipadd;
int port;
TcpListener tcplistener;
public async void startListeningForConnection(IPAddress ip = null, int dport = 23000)
{
if (ip == null)
{
ip = IPAddress.Any;
}
if (dport <= 0)
{
dport = 23000;
}
ipadd = ip;
port = dport;
tcplistener = new TcpListener(ipadd, port);
try
{
tcplistener.Start();
Console.WriteLine("Ready to accept client request.");
while (true)
{
TcpClient clientResponse =await tcplistener.AcceptTcpClientAsync();
HandleClientRequest(clientResponse);//method displays the message from client
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occured: {0}", ex.ToString());
}
}
private void HandleClientRequest(TcpClient acceptReturn)
{
NetworkStream stream = null;
StreamReader reader = null;
try
{
stream = acceptReturn.GetStream();
reader = new StreamReader(stream);
char[] buff = new char[64];
Console.WriteLine("***Ready to read");
while (true)
{
int nRet = reader.Read(buff, 0, buff.Length);
if (nRet == 0)
{
Console.WriteLine("Socket disconnected");
break;
}
string receivedText = new string(buff);
Console.Write(receivedText);
Array.Clear(buff, 0, buff.Length);
stream.Flush();
}
stream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Unknown Exception: {0}", ex.ToString());
}
finally
{
tcplistener.Stop();
Console.WriteLine("Exiting");
}
}
When i run startListeningForConnection(), I get this:
Output:
Ready to accept client request.
Press any key to continue . . .
it does not wait for a client to connect. If I remove the async and await then the program works as intended. Can anyone help me as it is my first socket program.
Server :
public class TcpServer
{
private TcpListener tcpListener;
private static ManualResetEvent allDone = new ManualResetEvent(false);
public TcpServer(string url, int port)
{
tcpListener = new TcpListener(IPAddress.Parse(url), port);
pingMapper = new LightPingMapper();
}
public void Run()
{
tcpListener.Start();
Console.WriteLine("Server running");
while (true)
{
allDone.Reset();
tcpListener.BeginAcceptSocket(AcceptCallback, tcpListener);
Console.WriteLine("Accepting socket");
allDone.WaitOne();
}
Console.ReadLine();
}
private void AcceptCallback(IAsyncResult result)
{
try
{
allDone.Set();
var listener = (TcpListener) result.AsyncState;
var handler = listener.EndAcceptSocket(result);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, state.buffer.Length, 0, ReadCallback, state);
}
catch (Exception e)
{
Console.WriteLine($"Error accepting callback. {e.Message}");
}
}
private void ReadCallback(IAsyncResult asyncResult)
{
try
{
string content = string.Empty;
Console.WriteLine("Read data from socket");
StateObject state = (StateObject) asyncResult.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(asyncResult);
if (bytesRead > 0)
{
state.sb.Append(Encoding.UTF8.GetString(state.buffer));
content = state.sb.ToString();
Console.WriteLine(content + " " + DateTime.Now);
}
}
catch (Exception e)
{
Console.WriteLine($"Error reading socket. {e.Message}");
}
}
}
class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
Client:
public class TCPClientWrapper
{
private TcpClient tcpClient;
private readonly string address;
private readonly int port;
public TCPClientWrapper(string address, int port)
{
InitTcpClient();
this.address = address;
this.port = port;
}
public void SendMessage()
{
for(int i=0; i < 10; i++)
{
if (!SocketConnected())
{
TryConnect();
}
byte[] buffer = Encoding.UTF8.GetBytes("Hello");
tcpClient.Client.Send(buffer);
Thread.Sleep(60000);
}
}
private void TryConnect()
{
bool isConnected = false;
while (true)
{
try
{
InitTcpClient();
tcpClient.Connect(IPAddress.Parse(address), port);
if (SocketConnected())
{
Console.WriteLine("TcpClient, Connected");
isConnected = true;
break;
}
}
catch (Exception e)
{
Console.WriteLine("TcpClient, connection failed. Try to reconnect after 30 seconds, {0}", e.Message);
}
finally
{
if (!isConnected)
{
tcpClient.Close();
Thread.Sleep(30000);
}
}
}
}
private void InitTcpClient()
{
tcpClient = new TcpClient();
tcpClient.SendTimeout = 15;
}
private bool SocketConnected()
{
var s = tcpClient.Client;
if (!s.Connected)
return false;
bool part1 = s.Poll(1000, SelectMode.SelectRead);
bool part2 = s.Available == 0;
return !(part1 && part2);
}
}
The problem is that server read only first message , each next message is not received by server . The tcpClient is connected , but server doesn't receive any message . Could anyone suggest what is wrong with my code ?
In ReadCallback, you don't start the next read - so yes, your code only reads once.
Adding
handler.BeginReceive(state.buffer, 0, state.buffer.Length, 0, ReadCallback, state);
to the bottom of ReadCallback (when bytesRead > 0) should work. However! You aren't implementing proper framing, so you should be very cautious of that. A basic framing implementation for a text-based protocol (like this) would be to use some kind of line-end sentinel, and buffer data until you see a line-end, then process the line.
On TCP, you are only guaranteed to get the right bytes in the right order (or a failed socket eventually) - you are not guaranteed to get them in the same composition in terms of calls to Send exactly matching calls to Receive in terms of the numbers of bytes in each.
I've been trying to send data from an android device to a computer by hosting a virtual network on the computer and connecting the android device to it (basically sending data over WLAN).
However, upon debugging the android app it always throws the following exception:
System.Net.Sockets.SocketException (0x80004005): Network subsystem is down.
Android App Code Snippet:
namespace TestWifiApp
{
[Activity(Label = "TestWifiApp", MainLauncher = true)]
public class MainActivity : Activity
{
string bssid = "00:18:39:12:2b:e9";
const string ssid = "TEST";
const string password = "123456abc";
const string kioskIp = "192.168.137.11";
const int kioskPort = 2201;
void sendData(string ntlId)
{
try {
System.Net.Sockets.Socket soc = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(kioskIp);
System.Net.IPEndPoint ep = new System.Net.IPEndPoint(ipAdd, kioskPort);
soc.Connect(ep);
byte[] data = ASCIIEncoding.ASCII.GetBytes(ntlId);
soc.Send(data);
soc.Disconnect(false);
soc.Close();
Log.Debug("TEST", "Data sent");
}
catch(Exception ex)
{
Log.Debug("TEST",ex.ToString());
}
}
}
Server Code:
class Program
{
const int PORT_NO = 2201;
static Socket serverSocket;
static void Main(string[] args)
{
//---listen at the specified IP and port no.---
Console.WriteLine("Listening...");
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT_NO));
serverSocket.Listen(4); //the maximum pending client, define as you wish
serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null);
string result = "";
do
{
result = Console.ReadLine();
} while (result.ToLower().Trim() != "exit");
}
private const int BUFFER_SIZE = 4096;
private static byte[] buffer = new byte[BUFFER_SIZE]; //buffer size is limited to BUFFER_SIZE per message
private static void acceptCallback(IAsyncResult result)
{ //if the buffer is old, then there might already be something there...
Socket socket = null;
try
{
socket = serverSocket.EndAccept(result); // The objectDisposedException will come here... thus, it is to be expected!
//Do something as you see it needs on client acceptance
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null); //to receive another client
}
catch (Exception e)
{ // this exception will happen when "this" is be disposed...
//Do something here
Console.WriteLine(e.ToString());
}
}
const int MAX_RECEIVE_ATTEMPT = 10;
static int receiveAttempt = 0; //this is not fool proof, obviously, since actually you must have multiple of this for multiple clients, but for the sake of simplicity I put this
private static void receiveCallback(IAsyncResult result)
{
Socket socket = null;
try
{
socket = (Socket)result.AsyncState; //this is to get the sender
if (socket.Connected)
{ //simple checking
int received = socket.EndReceive(result);
if (received > 0)
{
byte[] data = new byte[received]; //the data is in the byte[] format, not string!
Buffer.BlockCopy(buffer, 0, data, 0, data.Length); //There are several way to do this according to https://stackoverflow.com/questions/5099604/any-faster-way-of-copying-arrays-in-c in general, System.Buffer.memcpyimpl is the fastest
//DO SOMETHING ON THE DATA int byte[]!! Yihaa!!
Console.WriteLine(Encoding.UTF8.GetString(data)); //Here I just print it, but you need to do something else
//Message retrieval part
//Suppose you only want to declare that you receive data from a client to that client
string msg = "I receive your message on: " + DateTime.Now;
socket.Send(Encoding.ASCII.GetBytes(msg)); //Note that you actually send data in byte[]
Console.WriteLine("I sent this message to the client: " + msg);
receiveAttempt = 0; //reset receive attempt
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket); //repeat beginReceive
}
else if (receiveAttempt < MAX_RECEIVE_ATTEMPT)
{ //fail but not exceeding max attempt, repeats
++receiveAttempt; //increase receive attempt;
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket); //repeat beginReceive
}
else
{ //completely fails!
Console.WriteLine("receiveCallback fails!"); //don't repeat beginReceive
receiveAttempt = 0; //reset this for the next connection
}
}
}
catch (Exception e)
{ // this exception will happen when "this" is be disposed...
Console.WriteLine("receiveCallback fails with exception! " + e.ToString());
}
}
}
I've tried connecting another computer to the hosted network and sending data to that computer, but the same exception is thrown.
I am trying to write an Server application which is listening on a particular port and waiting for devices to hit that port.Device connects every 30 seconds Once a device connects the device sends its MAC address. But the problem is the memory keeps on increasing and never frees up.
class Server
{
Object threadLock = new Object();
bool stopListening = false;
Socket clientSocket = null;
private void StartDeviceListener()
{
try
{
// create the socket
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
// bind the listening socket to the port
IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, 60000);
clientSocket.LingerState = new LingerOption(false, 0);
clientSocket.Bind(ep1);
clientSocket.Listen(10); //Waiting for Devices to connect.
do
{
// start listening
Console.WriteLine("Waiting for device connection on {0}....", 60000);
Socket deviceSocket = clientSocket.Accept();
//Console.WriteLine(deviceSocket.
#region ThreadPool
// ThreadPool.QueueUserWorkItem(ProcessRequest, (Object)deviceSocket);
Thread ts = new Thread(ProcessRequest);
ts.IsBackground = true;
ts.Start((Object)deviceSocket);
ts.Join();
#endregion
} while (!stopListening);
}
catch (Exception ex)
{
Console.WriteLine("exception... : " + ex.Message);
StartDeviceListener();
}
finally
{
if (clientSocket != null) { clientSocket.Close(); clientSocket = null; }
}
}
public void Stop()
{
try
{
stopListening = true;
if (clientSocket != null)
{
clientSocket.Disconnect(false);
clientSocket = null;
}
}
catch (Exception ex)
{
Console.WriteLine("exception : " + ex.Message);
}
}
void ProcessRequest(Object args)
{
using (Socket deviceSocket = args as Socket)
{
try
{
//lock the thread while we are creating the client IO Interface Manager
lock (threadLock)
{
byte[] readBuffer = new byte[1024];
// Read from buffer
int count = deviceSocket.Receive(readBuffer, 0, readBuffer.Length, SocketFlags.None);
String macAddress = "";//mac address sent by the device:
if (count > 0)
{
Encoding encoder = Encoding.ASCII;
int size = 0;
while (count > 0)
{
size += count;
// get string
macAddress += encoder.GetString(readBuffer, 0, count).Trim();
// Read from buffer
count = 0;
}
Console.WriteLine(string.Format("{0} trying to connect....", macAddress));
}
deviceSocket.Close();
readBuffer = null;
}
//threadLock = null;
}
catch (Exception ex)
{
Console.WriteLine("exception : " + ex.Message);
}
}
args = null;
}
public void Start()
{
StartDeviceListener();
}
}`
But the problem is the memory keeps on increasing and never frees up.
That is still a far cry from a memory-leak. You're probably chasing a problem that does not exist.
In the last shot you still have a working set of 10MB, that's practically zero.
When you really want to find/solve memory issues, use a profiler.
This is a program to search for strings from a file. The string required by the client is given from the client side, in my case, using telnet. The program I have written is a server side one. It accepts multiple clients.
But, the problems I am unable rectify are-
It doesn't check for strings from the file.
As soon as the client gets connected, the client cannot type in the strings they want to search in that particular file.
It doesn't send the reply back (i.e. If the string is present in the file or not) to the client. Its only shown on the server side.
How do I proceed further? Could someone tell me where am I going wrong? Could someone please help me out with the code?
This is my try at the program..
class Program
{
static void Main(string[] args)
{
IPAddress ipad = IPAddress.Parse("192.168.0.181");
TcpListener serversocket = new TcpListener(ipad, 8888);
TcpClient clientsocket = default(TcpClient);
Byte[] bytes = new Byte[256];
serversocket.Start();
Console.WriteLine(">> Server Started");
while(true)
{
clientsocket = serversocket.AcceptTcpClient();
Console.WriteLine("Accepted Connection From Client");
LineMatcher lm = new LineMatcher(clientsocket);
Thread thread = new Thread(new ThreadStart(lm.Run));
thread.Start();
Console.WriteLine("Client connected");
}
Console.WriteLine(" >> exit");
Console.ReadLine();
clientsocket.Close();
serversocket.Stop();
}
}
public class LineMatcher //I've jumbled it up here. Don't know what exactly to do..
{
public string fileName = "c:/myfile2.txt";
private TcpClient _client;
public LineMatcher(TcpClient client)
{
_client = client;
}
public void Run()
{
try
{
StreamReader sr = new StreamReader("c:/myfile2.txt");
using (var reader = new StreamReader(_client.GetStream()))
{
string line ="";
int lineNumber = 0;
while (null != (line = sr.ReadLine()))
{
lineNumber += 1;
byte[] data = new byte[1024];
NetworkStream stream = _client.GetStream();
//if (line.Equals(line))
for (int ct = stream.Read(data,0, data.Length-1); 0 < ct; ct = stream.Read(data,0,data.Length-1))
line += Encoding.ASCII.GetString(data, 0, ct);
line = line.Trim();
{
lineNumber.ToString();
data = Encoding.ASCII.GetBytes(line);
_client.Client.Send(data, data.Length, SocketFlags.None);
Console.WriteLine("Line {0} matches {1}", lineNumber, line);
}
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
Console.WriteLine("Closing client");
_client.Close();
}
}
I think you got some pieces in your Run-method swapped - here is a version that should do the job:
public void Run()
{
byte[] data;
try
{
using (var r = new StreamReader("c:/myfile2.txt"))
{
string line ="";
int lineNumber = 0;
while (null != (line = r.ReadLine()))
{
data = Encoding.ASCII.GetBytes(line + "\n");
_client.Client.Send(data, data.Length, SocketFlags.None);
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
Console.WriteLine("Closing client");
_client.Close();
}
Please note that I'm not 100% sure what you are trying to do (I think you want your textfile send line-by-line to your Terminal) - so you might have to change some bits here and there.
Don't know where the Stream-messes in your code came from but I guess you tried various tutorials/snippets and forgot to clean up ;)