Why not just use "File" class?
Your server:
class Program
{
static void Main(string[] args)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iep);
server.Listen(10);
Console.WriteLine("Waiting for connection...");
using (Socket client = server.Accept())
{
while (true)
{
string s = Console.ReadLine().ToUpper();
if (s.Equals("QUIT"))
{
break;
}
if (s.Equals("SEND"))
{
byte[] buffer = File.ReadAllBytes("1.jpg");
client.Send(buffer, buffer.Length, SocketFlags.None);
Console.WriteLine("Send success!");
}
}
}
}
}
Your client:
class Program
{
static void Main(string[] args)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
client.Connect(iep);
while (true)
{
string input = Console.ReadLine();
if (input.ToUpper().Equals("QUIT"))
{
break;
}
else
{
byte[] buffer = new byte[1000000];
client.Receive(buffer, buffer.Length, SocketFlags.None);
Console.WriteLine("Receive success");
File.WriteAllBytes("1.jpg", buffer);
}
}
}
}
}