hi i have following code to send file and its giving file io error
Code:
namespace client_server
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener tcp = new TcpListener(ip, 1234);
tcp.Start();
Console.WriteLine("server started");
while (true)
{
Socket socket = tcp.AcceptSocket();
if (socket.Connected)
{
CHandler clH = new CHandler(socket);
Thread t = new Thread(new ThreadStart(clH.Task));
t.Start();
}
}
}
}
class CHandler
{
Socket socket;
public CHandler(Socket s) { socket = s; }
public void Task()
{
string path = "C:/";
NetworkStream net = new NetworkStream(socket);
StreamReader nIn = new StreamReader(net);
StreamWriter nOut = new StreamWriter(net);
string fName = nIn.ReadLine();
path = path + fName;
try
{
StreamReader fIn = new StreamReader(path);
string nextLine;
Console.WriteLine("sending file");
nextLine = fIn.ReadLine();
while (nextLine != null)
{
nOut.WriteLine(nextLine);
nOut.Flush();
nextLine = fIn.ReadLine();
}
Console.WriteLine("file sent");
fIn.Close();
net.Close();
nOut.Close();
socket.Close();
}
catch { Console.WriteLine("file IO error"); }
}
}
}
namespace clientside
{
class Program
{
static void Main(string[] args)
{
Console.Write("enter the file name");
string name = Console.ReadLine();
TcpClient serSocket;
try
{
serSocket = new TcpClient("127.0.0.1", 1234);
}
catch
{
Console.WriteLine("Failed to connect");
return;
}
NetworkStream net = serSocket.GetStream();
StreamWriter sOut = new StreamWriter(net);
StreamReader sIn = new StreamReader(net);
try
{
sOut.WriteLine(name);
sOut.Flush();
string nextLine = sIn.ReadLine();
while (nextLine != null)
{
Console.WriteLine(nextLine);
nextLine = sIn.ReadLine();
}
sIn.Close();
net.Close();
}
catch
{
Console.WriteLine("error reading from server");
}
Console.ReadKey();
}
}
}
Comment