I have created server application to accept connection from clients. After client connected to server ... they can send data and receive data between each other , but when another client is connected ... the server can not send data to frist client connected .
I need help how to save connected client on this server ... and how to send data to specified client.
The server class is :
namespace WindowsApplication11
{
public partial class Form1 : Form
{
private TcpListener tcpListener;
private Thread listenThread;
public Form1()
{
InitializeComponent();
}
TcpListener myList;
private void Form1_Load(object sender, EventArgs e)
{
try
{
myList = new TcpListener(IPAddress.Any, 8001);
/* Start Listeneting at the specified port */
myList.Start();
while (true)
{
TcpClient client = myList.AcceptTcpClient();
saveclient(" Client " + client.Client.RemoteEndPoint.ToString());
// - receive msg from client -
byte[] bb = new byte[10000];
int kb = client.Client.Receive(bb);
string stringu = "";
for (int i = 0; i < kb; i++)
stringu += Convert.ToChar(bb[i]);
// - send msg to accepted client -
Byte[] datat = System.Text.Encoding.ASCII.GetBytes("nje-> " + stringu);
NetworkStream stream = client.GetStream();
stream.Write(datat, 0, datat.Length);
stream.Flush();
}
}
catch (Exception ea)
{
Console.WriteLine("Error..... " + ea.Message);
}
}
void saveclient(string idlidhje)
{
try
{
System.IO.StreamWriter stw = System.IO.File.AppendText("d:\\clients.txt");
string teksti = System.String.Format("{0:G}: {1}.", System.DateTime.Now, idlidhje);
stw.WriteLine(teksti + "\n");
stw.Close();
}
catch (Exception eks)
{
}
}
}
}
The client class is :
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IPAddress addr = IPAddress.Any;
byte[] data = new byte[1024];
string input, stringData;
TcpClient server;
try
{
server = new TcpClient("127.0.0.1", 8001);
// - send msg -
Byte[] datat = System.Text.Encoding.ASCII.GetBytes("Hello");
NetworkStream stream = server.GetStream();
stream.Write(datat, 0, datat.Length);
stream.Flush();
// - receive msg -
byte[] bb = new byte[10000];
int kb = server.Client.Receive(bb);
string stringu = "";
for (int i = 0; i < kb; i++)
stringu += Convert.ToChar(bb[i]);
}
catch (SocketException fd)
{
MessageBox.Show("Cannot connect " + fd.Message.ToString());
}
}
Robert answered to a similar question here on SO:
https://stackoverflow.com/a/15878306/17646
Basically you have to keep the connected clients in a list (your TcpClient object 'client' gets lost every time the while loop starts again) and you need threads or async methods to handle the different clients.
Related
so made client-server-client tcp/ip app in c# that are supposed to communicate between 2 computers that are in different network.
I have few problems , first of them my server wont start on my public ip sayings its not valid in this context. Second is that I need to press send button twice before it sends data to server ,and then server 2 variations of data, one in bytes, other in char how its supposed to be. And third is how to make clients trying to receive data whole time. Should I have connection between clients have open whole time? I tried using timers to check if there is data to be received from the server.
Server code
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Parse("192.168.0.13"), 8080);
TcpClient client = default(TcpClient);
TcpClient client2 = default(TcpClient)
try
{
server.Start();
Console.WriteLine("Server started...");
}
catch(Exception ex)
{
Console.WriteLine("Server failed to start... {0}",ex.ToString());
Console.Read();
}
while (true)
{
client = server.AcceptTcpClient(); /
byte[] receivedBuffer = new byte[1000];
NetworkStream stream = client.GetStream();
stream.Read(receivedBuffer,0,receivedBuffer.Length);
StringBuilder message = new StringBuilder(); foreach (byte b in receivedBuffer)
{
if (b.Equals(126)
) //proveramo d {
break;
}
else
{
message.Append(Convert.ToChar(b).ToString()); }
}
client2 = server.AcceptTcpClient();
byte[] receivedBuffer2 = new byte[1000];
NetworkStream stream2 = client2.GetStream();
stream2.Read(receivedBuffer2, 0, receivedBuffer2.Length);
StringBuilder message2 = new StringBuilder();
foreach (byte g in receivedBuffer2)
{
if (g.Equals(126))
{
break;
}
else
{
message2.Append(g);
}
}
stream2.Write(receivedBuffer, 0, receivedBuffer.Length);
stream.Write(receivedBuffer2,0,receivedBuffer2.Length);
Console.WriteLine(message2.ToString());
Console.WriteLine(message.ToString());
}
Client code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
}
private int port = 8080;
private void submit_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient("192.168.0.13",port);
TcpListener listener = new TcpListener(IPAddress.Parse("192.168.0.13"), port);
long byteCount = Encoding.ASCII.GetByteCount(messagebox_TB.Text + 1); byte[] sentDataBytes = new byte[byteCount];
sentDataBytes = Encoding.ASCII.GetBytes(messagebox_TB.Text + "~");
NetworkStream stream = client.GetStream();
stream.Write(sentDataBytes,0,sentDataBytes.Length);
stream.Close();
client.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
long a = 0;
try
{
TcpClient client = new TcpClient("192.168.0.13", port);
TcpListener listener = new TcpListener(IPAddress.Parse("192.168.0.13"), port);
NetworkStream stream = client.GetStream();
byte[] receviedBytes = new byte[100];
stream.Read(receviedBytes, 0, receviedBytes.Length);
StringBuilder
message = new StringBuilder();
foreach (byte b in receviedBytes) {
if (b.Equals(126)
) //proveramo {
break;
}
else
{
message.Append(Convert.ToChar(b).ToString()); }
}
textBox1.Text = message.ToString();
client.Close();
stream.Close();
}
catch
{
a++;
}
}
}
I'm making a simple chat application using TcpClient and TcpServer from System.Net.
I got everything working on my PC, the server communicates with the client and vice versa. But when I try to connect to the same server application using another PC (even on the same subnet as my PC) the thing doesn't work.
I've tried port-forwarding through my router, firewall and still nothing.
The Can you see me website says that the connection is being refused. Although!! Sometimes it says that the connection is timed out completely.
I am not sure what's wrong with my code or my PC, I would really appreciate some help from people that are more experienced in this area than I am.
Here is the configuration for the port forwarding in my router:
If you need the code for the client and the server, here they are:
P.S. I make the change to the client in the BeginConnect() part at the bottom in order to change the IP address that I'm connecting to
Client:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace TcpTest_Client_
{
public partial class Form1 : Form
{
TcpClient client;
IPAddress localIP = null;
bool connected = false;
public Form1()
{
InitializeComponent();
}
public void clientConnectCallback(IAsyncResult result)
{
try
{
client.EndConnect(result);
Log("Connected to " + client.Client.RemoteEndPoint);
connected = true;
NetworkStream clientStream = client.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
//a socket error has occured
Log(ex.Message);
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
Log("Server on has disconnected");
break;
}
//message has successfully been received
UTF8Encoding encoder = new UTF8Encoding();
string bufferincmessage = encoder.GetString(message, 0, bytesRead);
Log("Server: " + bufferincmessage);
}
}
catch (Exception ex)
{
Log(ex.Message);
}
}
public void Log(string msg)
{
richTextBox1.BeginInvoke(new Action(
() =>
{
richTextBox1.Text += msg + "\n";
}));
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (IPAddress addr in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
localIP = addr;
break;
}
}
client = new TcpClient(AddressFamily.InterNetwork);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
client.BeginConnect(IPAddress.Parse("109.252.107.144"), 1234, clientConnectCallback, client);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (connected)
{
UTF8Encoding encoder = new UTF8Encoding();
NetworkStream clientStream = client.GetStream();
byte[] stringToSend = new byte[richTextBox2.Text.Length];
stringToSend = encoder.GetBytes(richTextBox2.Text);
clientStream.Write(stringToSend, 0, stringToSend.Length);
}
}
}
}
Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpTest
{
public partial class Form1 : Form
{
private TcpListener server;
private List<Thread> clientThreads = null;
public Thread MainThread;
public Form1()
{
InitializeComponent();
}
public void Log(string msg)
{
richTextBox1.BeginInvoke(new Action(
() =>
{
richTextBox1.Text += msg + "\n";
}));
}
public static string bufferincmessage;
public void AcceptSocketPackets(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
//a socket error has occured
Log(ex.Message);
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
Log("Client on " + tcpClient.Client.RemoteEndPoint + " has disconnected from the server");
break;
}
//message has successfully been received
UTF8Encoding encoder = new UTF8Encoding();
bufferincmessage = encoder.GetString(message, 0, bytesRead);
Log("Client from " + tcpClient.Client.RemoteEndPoint + ": " + bufferincmessage);
string stringToSend = "You sent me: " + bufferincmessage;
byte[] messageToSend = new byte[stringToSend.Length];
messageToSend = encoder.GetBytes(stringToSend);
clientStream.Write(messageToSend, 0, messageToSend.Length);
}
}
public void SocketAcceptCallback(IAsyncResult result)
{
TcpClient newClient = server.EndAcceptTcpClient(result);
Log("Accepted client on " + newClient.Client.RemoteEndPoint);
clientThreads.Add(new Thread(new ParameterizedThreadStart(AcceptSocketPackets)));
clientThreads[clientThreads.Count - 1].Start(newClient);
server.BeginAcceptTcpClient(SocketAcceptCallback, server);
}
private void Form1_Load(object sender, EventArgs e)
{
MainThread = Thread.CurrentThread;
clientThreads = new List<Thread>();
try
{
IPAddress localIP = null;
foreach (IPAddress addr in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
localIP = addr;
break;
}
}
localIP = IPAddress.Any;
// could also use this:
// server = new TcpListener(new IPEndPoint(IPAddress.Any, 1234));
server = new TcpListener(new IPEndPoint(localIP, 1234));
Log("Starting server on " + localIP + ":1234");
server.Start(6);
server.BeginAcceptTcpClient(SocketAcceptCallback, server);
Log("Started server on " + localIP + ":1234");
}
catch (Exception ex)
{
Log(ex.Message);
}
}
}
}
C++ is giving me problems like that. Try creating another project, paste the server code and build it as Release x86. If you change the build target and our problem is the same, windows firewall, even when turned off, won't allow the server or the client to run without any exception. If it doesn't work I might not know the answer.
My Client Source Code :
public partial class Form1 : Form
{
string serverip = "localHost";
int port = 160;
public Form1()
{
InitializeComponent();
}
private void Submit_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient(serverip, port);
int byteCount = Encoding.ASCII.GetByteCount(Message.Text);
byte[] sendData = new byte[byteCount];
sendData = Encoding.ASCII.GetBytes(Message.Text);
NetworkStream stream = client.GetStream();
stream.Write(sendData, 0, sendData.Length);
stream.Close();
client.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
My server
class Program
{
static void Main(string[] args)
{
IPAddress ip = Dns.GetHostEntry("localHost").AddressList[0];
TcpListener server = new TcpListener(ip, 160);
TcpClient client = default(TcpClient);
try
{
server.Start();
Console.WriteLine("The server has started successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
while (true)
{
client = server.AcceptTcpClient();
byte[] receivedBuffer = new byte[100];
NetworkStream stream = client.GetStream();
stream.Read(receivedBuffer, 0, receivedBuffer.Length);
StringBuilder msg = new StringBuilder();
foreach (byte b in receivedBuffer)
{
if (b.Equals(59))
{
break;
}
else
{
msg.Append(Convert.ToChar(b).ToString());
}
}
Console.WriteLine(msg.ToString() + msg.Length);
}
}
}
Essentially i want make it so i can have multiple clients on the server sending a message from different ip addresses of course. I have been using c# a year now mostly in school and I am above average at best at it.
First time asking question so sorry if its in wrong format
This is my first TCP listener program,
I could receive, parse and display data successfully from another PC.
But can you please check why this listener is not receiving another data ?
I want to update it everytime time when a client sends data. But its not updating once received data.
Here is my code:
public partial class FeederControlMonitor : Form
{
public string Status = string.Empty;
public Thread T = null;
public FeederControlMonitor()
{
InitializeComponent();
}
private void FeederControlMonitor_Load(object sender, EventArgs e)
{
label1.Text = "Server is Running...";
ThreadStart Ts = new ThreadStart(StartReceiving);
T = new Thread(Ts);
T.Start();
}
public void StartReceiving()
{
ReceiveTCP(9100);
}
public void ReceiveTCP(int portN)
{
TcpListener Listener = null;
try
{
Listener = new TcpListener(IPAddress.Any, portN);
Listener.Start();
}
catch (Exception ex)
{
File.WriteAllText(#"C:\\Drive\\ex.txt", ex.Message);
Console.WriteLine(ex.Message);
}
try
{
Socket client = Listener.AcceptSocket();
byte[] data = new byte[10000];
int size = client.Receive(data);
while (true)
{
client.Close();
ParseData(System.Text.Encoding.Default.GetString(data));
}
Listener.Stop();
}
catch (Exception ex)
{
File.WriteAllText(#"C:\\Drive\\ex.txt", ex.Message);
}
}
public void ParseData(string data)
{
var useFulData = data.Substring(data.IndexOf("F1")).Replace(" ", "");
useFulData = useFulData.Remove(useFulData.IndexOf("<ETX>"));
string[] delimeters = { "<DEL>", "<ESC>" };
var listOfValues = useFulData.Split(delimeters, StringSplitOptions.None).ToList();
int pos = 0;
for (int i = 1; i < listOfValues.Count; i += 2, pos++)
{
listOfValues[pos] = listOfValues[i];
}
listOfValues.RemoveRange(pos, listOfValues.Count - pos);
txtTubeName.Text = listOfValues[0];
txtCID.Text = listOfValues[1];
txtLocation.Text = listOfValues[2];
txtGender.Text = listOfValues[3];
txtAge.Text = listOfValues[4];
}
private void btnExit_Click(object sender, EventArgs e)
{
T.Abort();
this.Close();
}
}
Thanks in advance.
To much to explain where are errors. Here is simple multithread TcpServer.
// Initialize listener.
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
TcpClient client;
// Bind to address and port.
TcpListener listener = new TcpListener(address, 12345);
// Start listener.
listener.Start();
// In endless cycle accepting incoming connections.
// Actually here must be something like while(_keepWork)
// and on some button code to make _keepWork = false to
// stop listening.
while (true)
{
client = listener.AcceptTcpClient();
// When client connected, starting BgWorker
// use "using" statement to automatically free objects after work.
using (BackgroundWorker bgWorker = new BackgroundWorker())
{
// EventHandler.
bgWorker.DoWork += BgWorker_DoWork;
// Past client as argument.
bgWorker.RunWorkerAsync(client);
}
}
And method to handle connection (Edit: with read data part):
private static void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Get argument as TcpClient.
TcpClient client = e.Argument as TcpClient;
// Get stream from client.
NetworkStream netStream = client.GetStream();
// Input buffer to read from stream.
int inBuffSize = 1024;
byte[] inBuff = new byte[inBuffSize];
// Temporary buffer.
byte[] tempBuff;
// Result data recieved from client.
List<byte> data = new List<byte>();
// Read bytes from client into inputbuffer
int dataSize = netStream.Read(inBuff, 0, inBuffSize);
// If data recieved add to result.
while (dataSize > 0)
{
// Create new buffer.
tempBuff = new byte[dataSize];
// Copy data from inputBuffer to tempBuffer.
Array.Copy(inBuff, tempBuff, dataSize);
// Add to result.
data.AddRange(tempBuff);
// Read once more to check if any data still could be recieved.
dataSize = netStream.Read(inBuff, 0, inBuffSize);
}
}
So my job now is to create a server that can respond and connect to several client at once, that's why I create a threaded server and some test client to check.
The Client can connect to the server them by pressing a button, it will send a message to the server, the server will put to message to its interface then respond to the client. Lastly the client will receive the response and put it onto its interface.
The problem I got now is the server can receive the message from the server and display but the response from server to client is not successful even though they are similar. From the debugger it seems that the client is stuck at the Read procedure.
Below is my code for the server. I think the problem come at the connection handler and client side. I am stuck now, please show me what did I do wrong ##
public class Server
{
TcpListener serverSocket = new TcpListener(IPAddress.Any, 3000);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
private exampleCallback callback;
public Server(exampleCallback Callback)
{
callback = Callback;
}
public void initServer()
{
serverSocket.Start();
//Call back to main thread to update display
if (callback != null)
callback(">> Server Initialized!");
Console.WriteLine(">> Server Initialized!");
}
public void waitConnection()
{
int counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
if (callback != null)
callback(">> Client number: " + counter + " started!");
Console.WriteLine(">> Client number: " + counter + " started!");
handleClient handerler = new handleClient(new exampleCallback(receiveCallback), clientSocket, counter);
Thread clientThread = new Thread(new ThreadStart(handerler.startConv));
clientThread.Start();
}
}
public void closeServer()
{
clientSocket.Close();
serverSocket.Stop();
}
private void receiveCallback(string message)
{
callback(message);
}
}
The code for Connection Handerler:
public class handleClient
{
private exampleCallback callback2;
TcpClient clientSocket;
int clientNo;
public handleClient(exampleCallback Callback, TcpClient client, int clNo)
{
callback2 = Callback;
clientSocket = client;
clientNo = clNo;
}
public void startConv()
{
int requestCount = 0;
byte[] byteFrom = new byte[10025];
string XMLfromClient = null;
string dataFromClient = null;
string serverResponse = null;
string serverXMLResponse = null;
NetworkStream serverStream = clientSocket.GetStream();
requestCount = 0;
while (true)
{
try
{
requestCount++;
//Receive requerst from client
serverStream.Read(byteFrom, 0, (int)clientSocket.ReceiveBufferSize);
XMLfromClient = System.Text.Encoding.ASCII.GetString(byteFrom);
callback2("\n >> From Client :" + clientNo + ". Message: \n" + XMLfromClient);
//Console.WriteLine(">> From Client :" + clientNo + ". Message: \n" + XMLfromClient);
//Send acknowledgement to client
serverResponse = "Server to Client(" + clientNo + "). Request time: " + requestCount;
byte[] byteTo = Encoding.ASCII.GetBytes(serverResponse);
serverStream.Write(byteTo, 0, byteTo.Length);
serverStream.Flush();
Console.WriteLine(">> " + serverResponse);
}
catch (Exception ex)
{
callback2(ex.ToString());
}
}
}
}
And lastly is the code for client size:
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream;
public Form1()
{
InitializeComponent();
}
public void msg(string mesg)
{
this.Display.Text += Environment.NewLine + " >> " + mesg;
}
private void SendButton_Click(object sender, EventArgs e)
{
serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("World Domination");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
Thread receive = new Thread(new ThreadStart(ReceiveMess));
}
private void ReceiveMess()
{
serverStream = clientSocket.GetStream();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
msg("Data from Server : " + returndata);
}
private void connectButton_Click(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("127.0.0.1", 3000);
this.Display.Text = "Client Socket Program - Server Connected ...";
}
}
You seem to forget to start the receive thread:
private void SendButton_Click(object sender, EventArgs e)
{
// ... skipped .........
Thread receive = new Thread(new ThreadStart(ReceiveMess));
receive.Start(); // <--- try adding this line
}
server will put to message to its interface then respond to the client
So the server is a GUI app?
Based on the code you appear to have blocking operations (eg. AcceptTcpClient) on the GUI thread. This is going to fail. The GUI thread will block (and the UI will be unresponsive), which means updates to the IO from workers will block waiting on the UI (all UI operations must happen on the main thread).
Additionally spinning up a new thread for each connection is a bad idea. Threads are expensive.
You need to rewrite from the start (both client and server).
Avoid blocking operations (even file IO if you want maximum performance) on the GUI thread/
All socket operations should be run in the thread pool (let the system manage the threads for you).
Avoid waiting on the GUI from background threads (prefer BeginInvoke over Invoke).
Even better would be to use async: the system will manage the thread switching for you.