In this article we are going to see how to send a file from one computer to another using TCP protocol.
now in this example i am taking my own laptop as Client and server, so IpAddress are same when you want to send to different mention the correct IpAddress and binding Port Number.
For Server Side Code
C# :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace RServer
{
public partial class Form1 : Form
{
public delegate void Add(String Message);
Add mess;
public voidAddMessage(String Message)
{
listBox1.Items.Add(Message);
}
public Form1()
{
InitializeComponent();
mess = new Add(AddMessage);
}
private voidbutton1_Click(object sender, EventArgs e)
{
System.Threading.ThreadPool.QueueUserWorkItem(StartTcpServer);
}
public string GetIP()
{
string name = Dns.GetHostName();
IPHostEntry entry= Dns.GetHostEntry(name);
IPAddress[] addr = entry.AddressList;
if (addr[1].ToString().Split('.').Length == 4)
{
return addr[1].ToString();
}
return addr[2].ToString();
}
public void Message(string data)
{
listBox1.BeginInvoke(mess, data);
}
public void StartTcpServer(objectstate)
{
TcpListener filelistener = new TcpListener(IPAddress.Parse(GetIP()),8085);
filelistener.Start();
TcpClient client = filelistener.AcceptTcpClient();
Message("Client connection accepted from :" + client.Client.RemoteEndPoint + ".");
byte []buffer = new byte[1500];
int bytesread = 1;
StreamWriter writer = newStreamWriter("D:\\sample.rar");
while (bytesread > 0)
{
bytesread= client.GetStream().Read(buffer, 0, buffer.Length);
if (bytesread == 0)
break;
writer.BaseStream.Write(buffer, 0, buffer.Length);
Message(bytesread + " Received. ");
}
writer.Close();
}
}
}
C# :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
namespace RServer
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public string GetIP()
{
string name = Dns.GetHostName();
IPHostEntry entry = Dns.GetHostEntry(name);
IPAddress[] addr = entry.AddressList;
if (addr[1].ToString().Split('.').Length == 4)
{
return addr[1].ToString();
}
return addr[2].ToString();
}
private voidbutton1_Click(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader(textBox1.Text);
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(new IPEndPoint(IPAddress.Parse(GetIP()), 8085));
byte[] buffer = newbyte[1500];
long bytesSent = 0;
while (bytesSent < sr.BaseStream.Length)
{
int bytesRead = sr.BaseStream.Read(buffer, 0, 1500);
tcpClient.GetStream().Write(buffer, 0, bytesRead);
Message(bytesRead + " bytes sent.");
bytesSent += bytesRead;
}
tcpClient.Close();
Message("finished");
Console.ReadLine();
}
catch (Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
public void Message(string data)
{
listBox1.Items.Add(data);
}
private voidbutton2_Click(object sender, EventArgs e)
{
OpenFileDialog op = newOpenFileDialog();
op.ShowDialog();
textBox1.Text = op.FileName;
}
}
}
Server
Client
I Hope from this article you can learn how to send file from computer to another.