Hello guys this method sending file on client machine
private void StartServer()
{
TcpListener lsn = new TcpListener(IPAddress.Any, 27015);
Socket sck;
try
{
while (true)
{
lsn.Start();
sck = lsn.AcceptSocket();
byte[] b = new byte[100];
int k = sck.Receive(b);
string recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Version")
{
string _ipPort = sck.RemoteEndPoint.ToString();
var parts = _ipPort.Split(':');
_IPAddr = IPAddress.Parse(parts[0]);
_Port = Convert.ToInt32(parts[1]);
sck.Send(System.Text.Encoding.ASCII.GetBytes("1.1.0.0"));
}
k = sck.Receive(b);
recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Update")
{
fName = "Cannonball.mp3";
byte[] fileName = Encoding.UTF8.GetBytes(fName);
byte[] fileData = File.ReadAllBytes("D:\\Cannonball.mp3");
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
clientData = new byte[4 + fileName.Length + fileData.Length];
fileNameLen.CopyTo(clientData, 0);
fileName.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileName.Length);
if (sck.Connected == true)
{
sck.Send(clientData);
sck.Close();
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
When its goes to last if statment does not doing nothing. I wrote this code
Socket sck1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck1.Connect(_IPAddr, _Port);
sck1.Send(clientData);
but visual studio gives error that cannot establish connection.
I tried 999 port which i knew was open sck1.Connect(_IPAddr, 999); and client recieved file.
Does anyone know how i can send file that remote endpoint(sck.RemoteEndPoint) which server got ?
If
sck1.Connect(_IPAddr, 999)connects to some socket on the server’s machine, then at least the value of_IPAddris correct.Did you check what value
_Porthas when you are trying to connect? Does it match the port number the server is listening at?If these numbers match, you could try the following to test if your server is reachable: