I wrote a code doing file sharing well on local host but fails to do so when a non-local IP's are fed to it.
Here's the screen shot.
when i gave a copy of it to my colleague and asked him to be my server (just listen on my IP).He entered my machine IP and a pre-decided port and a click of Listen_button made the program crush.
I don't know why is this troubling me again and again.
Here's the brief code,hope i have provided enough information and u may ask me for more (if needed).
Server:
Code:
private void btnlisten_Click_1(object sender, EventArgs e)
{
ThreadStart threaddelegate = new ThreadStart(Thread);
Thread newthread = new Thread(threaddelegate);
newthread.Start();
}
public void Thread()
{
IPAddress ipaddr = IPAddress.Parse(IPAddr_Serv.Text);
var port = Convert.ToInt32(PortNo_serv.Text);
MessageBox.Show(port + ipaddr.ToString());
TcpListener tcpListener = new TcpListener(ipaddr, port);
tcpListener.Start();
MessageBox.Show("Listening on port" + port);
TcpClient client = new TcpClient();
// Accept client
client = tcpListener.AcceptTcpClient();
NetworkStream netStream = client.GetStream();
string DirName = @"D:";
string fileloc = DirName+FileName;
Directory.CreateDirectory(Path.GetDirectoryName(fileloc));
using (FileStream fs = new FileStream(fileloc, FileMode.OpenOrCreate, FileAccess.Write))
{
// do file receiving chores here
}
Client:
Code:
TcpClient clientSocket = new TcpClient();
// client.Connect(IPAddr_Client.Text, 8004);
IPAddress ipaddrcl = IPAddress.Parse(IPAddr_Client.Text);
int port = Convert.ToInt32(PortNo_serv.Text);
// Connect to server
try
{
clientSocket.Connect(new IPEndPoint(ipaddrcl,port));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
NetworkStream netstream = clientSocket.GetStream();
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
//file receivng code goes here
}
PS. A button_click makes server to run in a thread and listens to client.
Suggestions please