I am new to programming. For my first application I decided to create two console applications - server and client application. I want to test data transaction bandwith between client and server app(by the way I am using sockets). The problem I have is that I need to test connection speed for, lets say 10 seconds and after those 10 seconds I need to get received data amount (which I have received in this time period) and calculate speed...How can I do that?
Client app
namespace Example_01_Sockets_Client
{
class MainClass
{
public static void Main(string[] args)
{
try
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ownAddress = IPAddress.Parse("127.0.0.1");
var ownEndpoint = new IPEndPoint(ownAddress, 4321);
socket.Bind(ownEndpoint);
Console.WriteLine();
Console.WriteLine("Trying to connect to server...");
var serverAddress = IPAddress.Parse("127.0.0.1");
socket.Connect(serverAddress, 2222);
Console.WriteLine("Connected to server");
var buffer = new byte[1024 * 150000];
socket.ReceiveTimeout = 100;
int receivedBytesLen = socket.Receive(buffer);
Console.WriteLine("Download speed: " + ((receivedBytesLen) / 100 + "kb/s ") );
Console.WriteLine("Press any key to exit");
Console.ReadKey();
socket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Server App
class MainClass
{
public static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
var listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndpoint = new IPEndPoint(ipAddress, 2222);
listeningSocket.Bind(localEndpoint);
listeningSocket.Listen(1);
Console.WriteLine();
Console.WriteLine("Waiting for client...");
Socket connectedSocket = listeningSocket.Accept();
listeningSocket.Close();
string clientAddress = connectedSocket.RemoteEndPoint.ToString();
Console.WriteLine("Client connected (" + clientAddress + ")");
string fileName = "downTest.txt";
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
connectedSocket.Send(fileData);
//int bytesReceived = connectedSocket.Receive();
Console.WriteLine("Client connected (" + clientAddress + ")");
connectedSocket.Close();
}
}
}
Thanks for any help!!!
Why not use a set amount of data (eg. 4,096kb). Use the Stopwatch class to start a timer right before you start downloading data on the client, and then stop the timer immediately after it finishes sending. You can then use the elapsed time property to determine how long it took to send that fixed amount of data.
Stopwatch timer = new Stopwatch();
timer.Start();
// Download data here
timer.Stop();
int elapsedTime = timer.Elapsed.TotalSeconds;
Related
i have a problem i am unable to resolve as my c# knowledge is not very good.
I found some code on the internet and modified it according to my needs.
My problem is that when i send messages to clients only one receives the message, then the next message is received by another client and so on.
I want to send same message to all connected clients without losing any data.
Server
using System;
using System.Net.Sockets;
using System.Threading;
public class AsynchIOServer
{
static TcpListener tcpListener = new TcpListener(10);
static void Listeners()
{
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
Console.WriteLine("Client:"+socketForClient.RemoteEndPoint+" now connected to server.");
NetworkStream networkStream = new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
////here we send message to client
while (true){
Console.WriteLine("type your message to be recieved by client:");
string theString = Console.ReadLine();
streamWriter.WriteLine(theString);
////Console.WriteLine(theString);
streamWriter.Flush();
}
streamReader.Close();
networkStream.Close();
streamWriter.Close();
}
socketForClient.Close();
Console.WriteLine("Press any key to exit from server program");
Console.ReadKey();
}
public static void Main()
{
//TcpListener tcpListener = new TcpListener(10);
tcpListener.Start();
Console.WriteLine("************This is Server program************");
Console.WriteLine("Hoe many clients are going to connect to this server?:");
int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());
for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
{
Thread newThread = new Thread(new ThreadStart(Listeners));
newThread.Start();
}
}
}
Client:
using System;
using System.Net.Sockets;
using System.Threading;
public class Client
{
static public void Main(string[] Args)
{
TcpClient socketForServer;
try
{
socketForServer = new TcpClient("localHost", 10);
}
catch
{
Console.WriteLine(
"Failed to connect to server at {0}:999", "localhost");
return;
}
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
Console.WriteLine("*******This is client program who is connected to localhost on port No:10*****");
try
{
string outputString;
// read the data from the host and display it
{
while (true)
{
outputString = streamReader.ReadLine();
Console.WriteLine("Message Recieved by server:" + outputString);
streamWriter.Flush();
}
}
}
catch
{
Console.WriteLine("Exception reading from Server");
}
// tidy up
networkStream.Close();
Console.WriteLine("Press any key to exit from client program");
Console.ReadKey();
}
private static string GetData()
{
//Ack from sql server
return "ack";
}
}
Thank you
simple working multi-threaded server:
static void Process(Socket client) {
Console.WriteLine("Incoming connection from " + client.RemoteEndPoint);
const int maxMessageSize = 1024;
byte[] response;
int received;
while (true) {
// Send message to the client:
Console.Write("Server: ");
client.Send(Encoding.ASCII.GetBytes(Console.ReadLine()));
Console.WriteLine();
// Receive message from the server:
response = new byte[maxMessageSize];
received = client.Receive(response);
if (received == 0) {
Console.WriteLine("Client closed connection!");
return;
}
List<byte> respBytesList = new List<byte>(response);
respBytesList.RemoveRange(received, maxMessageSize - received); // truncate zero end
Console.WriteLine("Client (" + client.RemoteEndPoint + "+: " + Encoding.ASCII.GetString(respBytesList.ToArray()));
}
}
static void Main(string[] args) {
int backlog = -1, port = 2222;
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.ReceiveTimeout = -1;
// Start listening.
try {
server.Bind(new IPEndPoint(IPAddress.Any, port));
server.Listen(backlog);
}
catch (Exception) {
Console.WriteLine("Listening failed!");
Console.ReadKey();
return;
}
Console.WriteLine("Start listening...");
while(true) {
Socket client = server.Accept();
new System.Threading.Thread(() => {
try { Process(client); } catch (Exception ex) { Console.WriteLine("Client connection processing error: " + ex.Message); }
}).Start();
}
//Console.WriteLine("Press any key for exit...");
//Console.ReadKey();
}
And client:
static void WorkWithServer(Socket server) {
const int maxMessageSize = 1024;
byte[] response;
int received;
while(true) {
try {
// Receive message from the server:
response = new byte[maxMessageSize];
received = server.Receive(response);
if (received == 0) {
Console.WriteLine("Server closed connection.");
return;
}
List<byte> respBytesList = new List<byte>(response);
respBytesList.RemoveRange(received, maxMessageSize - received); // truncate zero end
Console.WriteLine("Server: " + Encoding.ASCII.GetString(respBytesList.ToArray()));
// Send message to the server:
Console.Write("You: ");
server.Send(Encoding.ASCII.GetBytes(Console.ReadLine()));
Console.WriteLine();
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
return;
}
}
}
static void Main(string[] args) {
IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 2222);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.ReceiveTimeout = -1;
// Connect to the server.
try { server.Connect(serverEp); }
catch (Exception) {
Console.WriteLine("Establish connection with server (" + serverEp + ") failed!");
Console.ReadKey();
return;
}
Console.WriteLine("Connection with server (" + serverEp + ") established!");
WorkWithServer(server);
Console.WriteLine("Press any key for exit...");
Console.ReadKey();
}
Edit as you need.
Your code is wrong.
First of all I'll give you some insight of how it's wrong and why it's not working the way you want.
You're creating n number of threads, and making them ALL wait for a connection. What if you exhaust the number of threads? What if any of them exits unexpectedly?
You are also sending the data wrong. By using Console.ReadLine you aren't passing the data through multiple threads and reading the line in each one, instead, the first one that calls Console.ReadLine will be the one that's going to receive it. This means you'll only be sending to only one Socket.
It's not ideal how you're managing this. There are dozens if not hundreds of multithreaded socket server/client available online, and I'd invite you to research a little bit more. But first I'd like you to research more about Thread/Task in C#.
I created a .NET class socket using a websocket server. When my browser tries to connect to socket on my program, I see that the method 'accept socket' is called very slow or seconds after my browser connects. I tried creating many connections but the socket accepts a websocket every second. I tried alchemy websocket too, results like my code.
After I used Miscrosoft websocket on IIS8, I noticed that the speed is pretty good.
I am not experimental.
My code (express)
class Connection
{
byte[] buffer = new byte[1024];
Socket socket;
bool IsAuthencation;
public Connection(Socket socket)
{
this.socket = socket;
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
void OnReceive(IAsyncResult result)
{
try
{
int count = socket.EndReceive(result);
if (count != 0)
{
if (!IsAuthencation)
{
byte[] tmp = new byte[count];
Array.Copy(buffer, 0, tmp, 0, count);
string Request = Encoding.UTF8.GetString(tmp);
if (Request.Contains("GET"))
{
if (!IsAuthencation)
{
int indexStart = Request.IndexOf("Sec-WebSocket-Key: ");
int indexEnd = Request.IndexOf("Sec-WebSocket-Version:");
string key = Request.Substring(indexStart + 19, indexEnd - indexStart - 21);
string magic = string.Concat(key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
string base64 = "";
using (SHA1 sha1 = SHA1.Create())
{
byte[] bufferMagic = sha1.ComputeHash(Encoding.UTF8.GetBytes(magic));
base64 = Convert.ToBase64String(bufferMagic);
}
Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
+ "Connection: Upgrade" + Environment.NewLine
+ "Upgrade: websocket" + Environment.NewLine
+ "Sec-WebSocket-Accept: " + base64
+ Environment.NewLine
+ Environment.NewLine);
socket.BeginSend(response, 0, response.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
IsAuthencation = true;
}
}
}
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
else
{
// disconnect
}
}
catch (Exception)
{
}
}
void OnSend(IAsyncResult result)
{
try
{
socket.EndSend(result);
}
catch (Exception)
{
}
}
}
class Program
{
static List<Connection> clients;
static Socket socket;
static void Main(string[] args)
{
clients = new List<Connection>();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 30000);
socket.Bind(endPoint);
socket.Listen(0);
socket.BeginAccept(new AsyncCallback(AcceptAsync), null);
Console.ReadLine();
}
static void AcceptAsync(IAsyncResult result)
{
Console.WriteLine("a new connection");
Socket s = socket.EndAccept(result);
clients.Add(new Connection(s));
socket.BeginAccept(new AsyncCallback(AcceptAsync), null);
}
}
you can try command javascript var socket= new WebSocket("ws://localhost:30000"), i not see a problem in my code.
Thanks
You have to accept the socket, and handle it asynchronously, and then back to accepting more sockets straightaway.
UPDATE
After seeing your code, it seems you are accepting connections asynchronously. How are you creating those test connections? If you are creating too many you are maybe saturating the app.
I think I see the problem, why are you setting the socket backlog to 0? Read this answer from SO.
Try to put a bigger value: socket.Listen(100);
I want to send a message from a C# application to the Android emulator or device over TCP. I have searched on Google and got the maximum results for Android client and C# server but not what I want to acheive.
Here is what I have tried; maybe I am going about it the wrong way but what I want is to send a message over TCP to an Android device.
Server Code to send a message:
private static int port = 4444;
private static TcpListener listener;
private static Thread thread;
private static int clientId = 0;
listener = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);
thread = new Thread(new ThreadStart(Listen));
thread.Start();
private static void Listen()
{
listener.Start();
MessageBox.Show("Listening on: " + port.ToString());
while (true)
{
MessageBox.Show("Waiting for connection....");
MessageBox.Show("Client No: " + clientId);
TcpClient client = listener.AcceptTcpClient();
Thread listenThread = new Thread(new ParameterizedThreadStart(ListenThread));
listenThread.Start(client);
}
}
private static void ListenThread(Object client)
{
NetworkStream netstream = ((TcpClient)client).GetStream();
MessageBox.Show("Request made");
clientId = clientId + 1;
// String message = "Hello world";
byte[] resMessage = Encoding.ASCII.GetBytes(clientId.ToString());
netstream.Write(resMessage, 0, resMessage.Length);
netstream.Close();
}
Client Code
private TextView textDisplay;
Socket socket;
private static final int TCP_SERVER_PORT = 4444;
ServerSocket ss = null;
try {
ss = new ServerSocket(TCP_SERVER_PORT);
//ss.setSoTimeout(10000);
//accept connections
Socket s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//receive a message
String incomingMsg = in.readLine() + System.getProperty("line.separator");
Log.d("TcpServer", "received: " + incomingMsg);
textDisplay.append("received: " + incomingMsg);
//send a message
String outgoingMsg = "goodbye from port " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outgoingMsg);
out.flush();
Log.d("TcpServer", "sent: " + outgoingMsg);
textDisplay.append("sent: " + outgoingMsg);
//SystemClock.sleep(5000);
s.close();
} catch (InterruptedIOException e) {
//if timeout occurs
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
Log.d("Error",e.toString());
}
}
}
The problem lies in your Client code.
A socket is opened between two parties, one that plays the role of the listener / server that does bind->listen->accept and a client that connect
In your code there are two problems:
Your client should connect and not accept
Both your client and your server will try to read from the socket and write afterwards. There would be no problem if your read and write operations from the socket were on different threads.In your case this will result in a deadlock.
If you want both operations to be done on the same thread , sequentially then your client and server should perform the read and write in opposite order :
Client : read -> write
Server: write-> read
I have a problem with my server socket code below - the loop in the Main() method is only executing once, never accepting any further input.
class Server
{
public Socket servSock(int Port)
{
Socket s1 = null;
IPHostEntry ipHE = Dns.GetHostEntry("localhost");
IPAddress ipA = ipHE.AddressList[0];
IPEndPoint ipEp = new IPEndPoint(ipA, Port);
s1 = new Socket(ipEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s1.Bind(ipEp);
return s1;
}
const int BUFFSIZE = 1024;
const int BACKLOG = 255;
static void Main(string[] args)
{
Nwork nwork = new Nwork();
Socket cl = null;
Socket s = nwork.servSock(400);
s.Listen(BACKLOG);
byte[] rcvBuffer = new byte[BUFFSIZE];
for (; ; )
{
string text = "";
Console.Clear();
cl = s.Accept();
Console.Write("Handling Client >> " + cl.RemoteEndPoint +"\n\n\n");
cl.Receive(rcvBuffer, BUFFSIZE, SocketFlags.None);
text = Encoding.ASCII.GetString(rcvBuffer, 0, BUFFSIZE).TrimEnd('\0');
Console.Write(text);
cl.Close();
}
}
}
I don't know what an "Nwork" is, but I suspect that's the source of your bug. Just initializing the socket manually works for me and allows subsequent connections.
static void Main(string[] args)
{
Socket s = new Socket(SocketType.Stream, ProtocolType.TCP);
Socket cl = null;
System.Net.IPEndPoint endpoint = new System.Net.IPEndPoint(0, 400); // listen on all adapters on port 400
s.Bind(endpoint);
s.Listen(BACKLOG);
byte[] rcvBuffer = new byte[BUFFSIZE];
for (; ; )
{
string text = "";
Console.Clear();
cl = s.Accept();
Console.Write("Handling Client >> " + cl.RemoteEndPoint + "\n\n\n");
cl.Receive(rcvBuffer, BUFFSIZE, SocketFlags.None);
text = Encoding.ASCII.GetString(rcvBuffer, 0, BUFFSIZE).TrimEnd('\0');
Console.Write(text);
cl.Close();
}
}
Without deep diving in your code i would try adding the s.Listen(BACKLOG) in your for loop.
Note that this way you can only process i socket connection at the same time, if another person is gonna try and open a socket connection your process will be busy receiving the data from a previous connection.
It depends on your scenario but you might want to implement it asynchronous.
This link might be usefull if you decided to do so.
I can not send data stunserver remote computer using the two. Data comes from local computers, but data on remote computers is not going to come.
I'm using my program, stunserver
public void run()
{
UpdateText("Now Listening..");
remoteSender = new IPEndPoint(IPAddress.Any, 0);
tempRemoteEP = (EndPoint)remoteSender;
byte[] packet = new byte[1024];
while (true)
{
if (socket.Available > 0)
{
this.nbBytesRx = socket.ReceiveFrom(packet, ref tempRemoteEP);
nbPackets++;
seqNo = BitConverter.ToInt16(packet, 0);
UpdateText(nbPackets.ToString() + ":" + seqNo.ToString() + " / ");
}
}
}
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
string localEP = socket.LocalEndPoint.ToString();
string publicEP = "";
string netType = "";
STUN_Result result = STUN_Client.Query("stunserver.org", 3478, socket);
netType = result.NetType.ToString();
if (result.NetType != STUN_NetType.UdpBlocked)
{
publicEP = result.PublicEndPoint.ToString();
}
else
{
publicEP = "";
}
UpdateText("Local EP:" + localEP);
UpdateText("Public EP:" + publicEP);
ThreadStart startMethod = new ThreadStart(this.run);
thread = new Thread(startMethod);
thread.Start();
I am working on the same problem, and using the same library.
Did you check if you get your endpoint? I found our that pointed server wasn`t online. So I made a List of servers to go throught.
My method to get public EP:
public static IPEndPoint GetMyPublicEP() {
// Create new socket for STUN client.
Socket _socket = new Socket
(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_socket.Bind(new IPEndPoint(IPAddress.Any, 0));
List<string> _stunServers = new List<string> { // список стан серверов для проверки своей ендпоинт, не все работают
"stun.l.google.com:19302",
"stun1.l.google.com:19302",
"stun2.l.google.com:19302",
"stun3.l.google.com:19302",
"stun4.l.google.com:19302",
"stun01.sipphone.com",
"stun.ekiga.net",
"stun.fwdnet.net",
"stun.ideasip.com",
"stun.iptel.org",
"stun.rixtelecom.se",
"stun.schlund.de",
"stunserver.org",
"stun.softjoys.com",
"stun.voiparound.com",
"stun.voipbuster.com",
"stun.voipstunt.com",
"stun.voxgratia.org",
"stun.xten.com"
};
foreach (string server in _stunServers)
{
try
{
STUN_Result result = STUN_Client.Query(server, 3478, _socket);
IPEndPoint myPublicEPStun = result.PublicEndPoint;
if (myPublicEPStun != null)
{
_myPublicEPStun = myPublicEPStun;
break;
}
}
catch (Exception E)
{
Console.WriteLine("Якась необ`ясніма магія трапилась");
}
}
return _myPublicEPStun;
}
I had the same problem... i solved it disabling Windows Firewall. It was blocking all the traffic.