I want to make a program so that the server reads from a file (c:/test.txt) and send data to the client, and it writes to another file (c:/info2.txt).
The problem this poses is that I could not figure out that when the client stops receiving, it should call me to send nothing but how to do a program like that?
here is the my source code
Client:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Script_Client
{
class Program
{
static void Main(string[] args)
{
TcpClient c = new TcpClient("localhost", 8080);
Stream str = c.GetStream();
byte[] b=new byte[100];
string line = null;
StreamWriter fluxInfos = null;
try
{
fluxInfos = new StreamWriter("c:/infos2.txt" );
while(str.Read(b,0,50)!=-1)
{
line = Encoding.ASCII.GetString(b);
Console.WriteLine(line);
fluxInfos.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The following error occurred: " + e.Message);
}
Console.ReadLine();
}
}
}
Server:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Play
{
class Program
{
static void Main(string[] args)
{
IPHostEntry iphe = Dns.Resolve("localhost" );
IPEndPoint ipep = new IPEndPoint(iphe.AddressList[0], 8080);
TcpListener s = new TcpListener(ipep);
s.Start();
TcpClient c = s.AcceptTcpClient();
Stream str = c.GetStream();
byte[] b;
String line = null;
StreamReader fluxInfos = null;
try
{
fluxInfos = new StreamReader("c:/Test.txt" );
line = fluxInfos.ReadLine();
while (line != null)
{
Console.WriteLine(line);
b = Encoding.ASCII.GetBytes(line);
str.Write(b, 0, b.Length);
line = fluxInfos.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("The following error occurred: " + e.Message);
}
System.Console.ReadLine();
}
}
}
Bookmarks