I recently started writing a program for sending a file from one system to another using the TCPClient
protocol and .Net
.
The program has a client method and a server method. Client for sending the data and server to receive and build the file at the other end.
When I initially wrote the program, I did so with Winforms
and everything was working perfectly. Since then, I tried to move everything over to UWP
but I've had nothing but issues since doing so.
I've managed to fix most of the issues but I am having an issue I can't seem to get past. It appears as though the program is not accepting any TCP
connections.
If I send from the Winforms
version to the UWP
version it fails, but doing it reversely (sending from UWP
to the Winforms
version) everything works. This also means that I am unable to send from the UWP
to the UWP
version.
Any advice would be brilliant, I've attached the method for the server side and the client side.
Thanks!
private void btnAcceptConn_Tapped(object sender, TappedRoutedEventArgs e)
{
try
{
string hostName = Dns.GetHostName();
//gets the IP and port number from text boxes
Int32 server_port = Int32.Parse(txtboxPortNumber.Text);
IPAddress localAdd = IPAddress.Parse(txtboxServerIP.Text);
//listen on the specific IP and port no
TcpListener listener = new TcpListener(localAdd, server_port);
Byte[] bytes = new Byte[256];
byte[] fullarrays = new byte[0];
listen = true;
while (listen == true)
{
//write to log
string LogMess = "Listening...";
WriteToLog(LogMess);
listener.Start();
//incoming client connected
TcpClient client = listener.AcceptTcpClient();
string LogMessConn = "Connected!";
// WriteToLog(LogMessConn);
//gets incoming data through network stream
NetworkStream nwStream = client.GetStream();
int off = 0;
Thread.Sleep(200);
//loop to receive all the data sent by the client
while (nwStream.DataAvailable)
{
nwStream.Read(bytes, 0, bytes.Length);
Array.Resize(ref fullarrays, fullarrays.Length + bytes.Length);
Buffer.BlockCopy(bytes, 0, fullarrays, off, bytes.Length);
off = +fullarrays.Length;
}
CreateFile(fullarrays);
string LossMessComplete = "Complete Transfer";
WriteToLog(LossMessComplete);
client.Close();
listener.Stop();
listen = false;
}
}
catch (Exception)
{
/*lblError.Visible = true;
lblErrorMSG.Visible = true;
lblErrorMSG.Text = "Please check enterted details";*/
}
}
private void btnSend_Data_Tapped(object sender, TappedRoutedEventArgs e)
{
Int32 server_port = Int32.Parse(txtboxPortNumber.Text);
string ServerIP = txtboxServerIP.Text;
//puts the file into a bytes array. gets the filetosendpath from the file select button
Byte[] textToSend = filetobytes(filetosendpath);
//creates a TCP client object at the IP and port number
TcpClient client = new TcpClient(ServerIP, server_port);
client.SendTimeout = 1600000;
client.ReceiveTimeout = 1600000;
NetworkStream nwStream = client.GetStream();
//byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//var bytesToSend = textToSend;
//send the text
string LogMess = "Sending: " + textToSend;
WriteToLog(LogMess);
load_activitylog();
nwStream.Write(textToSend, 0, textToSend.Length);
}