kernal kernal - 25 days ago 10
C# Question

How I can send message from server to client using the port?

I am using Tcplistener to listen on a port for requests. When the requests come in from the client I want to know the client ip making the request.
the problem is : the server and all client take the same Ip this is picture show you the execution :
here the server and all clients take the same ip

so I decided to send massage from server to client by port ... I search a lot on google But I did not knew how I can send message from server to client using the port ? so can any body help me ?
My code is :
the client code is :

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace clients
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
Stream fileStream = File.OpenRead(tbFilename.Text);
// Alocate memory space for the file
byte[] fileBuffer = new byte[fileStream.Length];
fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
// Open a TCP/IP Connection and send the data
TcpClient clientSocket = new TcpClient(tbServer.Text,8080);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(fileBuffer,0,fileBuffer.GetLength(0));
networkStream.Close();


}

private void btnbrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
tbFilename.Text = openFileDialog1.FileName;
}
}
}


the code of server :

using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;

namespace server
{
public partial class Form1 : Form
{
private ArrayList nSockets;
public Form1()
{
InitializeComponent();
}

private void lbstatues_Click(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();
nSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(listenerThread));
thdListener.Start();


}
public void listenerThread()
{
TcpListener tcpListener = new TcpListener(8080);
tcpListener.Start();
while(true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
Control.CheckForIllegalCrossThreadCalls = false;
lbConnections.Items.Add(
handlerSocket.RemoteEndPoint.ToString() + " connected." );
// ((IPEndPoint)handlerSocket.RemoteEndPoint).Address.ToString());
lock (this)
{
nSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new
ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}

public void handlerThread()
{
Socket handlerSocket = (Socket)nSockets[nSockets.Count-1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int thisRead=0;
int blockSize=1024;
Byte[] dataByte = new Byte[blockSize];
lock(this)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite("D:\\SubmittedFile.txt");
while(true)
{
thisRead=networkStream.Read(dataByte,0,blockSize);
fileStream.Write(dataByte,0,thisRead);
if (thisRead==0) break;
}
fileStream.Close();
}
lbConnections.Items.Add("File Written");
handlerSocket = null;
}


}
}

Answer

The clients are not listening for incoming connections, so the only way for the server to send a message to a client is by using an existing connection, that was previously established by the client.

You don't have to specify the port number when sending a message on an existing connection, your server can just use:

Socket handlerSocket = tcpListener.AcceptSocket();
// a connection is now established, "handlerSocket" can be used for both receiving and sending messages
handlerSocket.Send(...);

Your clients currently send data and immediately close the connection though, you'll have to modify them to read data from the NetworkStream in order to receive anything.

There are tons of tutorials available, I think you just googled the wrong keywords. Have you tried something like c# chat tutorial?