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 void
AddMessage(String Message)
{
listBox1.Items.Add(Message);
}
public Form1()
{
InitializeComponent();
mess = new Add(AddMessage);
}
private void
button1_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(object
state)
{
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 = new
StreamWriter("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 void
button1_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 = new
byte[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 (Exception
ex)
{
MessageBox.Show(ex.Message);
}
}
public void Message(string data)
{
listBox1.Items.Add(data);
}
private void
button2_Click(object sender, EventArgs e)
{
OpenFileDialog op = new
OpenFileDialog();
op.ShowDialog();
textBox1.Text = op.FileName;
}
}
}
Server
Client
I Hope from this article you can learn how to send file from computer to another.
can u please forward me the code on my account
ReplyDeletepembu01@gmail.com
It would be so great of u
it was so helpful sir for your codes it worked out and it's working
ReplyDeleteHi pemboo00,
ReplyDeleteYou can take this code and make a winforms application by paste the code just change the namespace for the application.
i got this error "Only one usage of each socket address (protocol/network address/port) is normally permitted" how to rectify it
ReplyDeleteCheck once whether the socket:port is already in use for some other application !! use this cmd
ReplyDeletenetstat -a -b
Which wil list all port in use and the application exe name
Hi sir, can u forward me the code, keith_dizon0529@yahoo.com to be my reference, it will be a great help, thanks
ReplyDeleteJust copy paste the above code in ur application , it works well . . .Tested one
DeleteSir, I have tried your code it sends a file but when i extract that file the file is corrupted.
ReplyDeleteHi,
DeleteThe Code works well, i didn't thing so file may be corrupt, because the data is transferring as byte by byte, so corrupt only possible in huge transfer in single send and receive data. Can you please catch the error along with stack trace and post here , then only i can debug the bug and able to give the solution.
The file size is 2.5 MB. At receiving side it shows size as either 3 MB or 90KB. Hence while doing unzip it gives error
DeleteCan you paste the code that you have written by using this code
DeleteWill this code also enable file sharing over the internet?
ReplyDeleteAccess to the path 'C:\sample.rar' is denied.
ReplyDeletethanks for sharing nice trick. descargar cshare apk
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSir Raj Esh. Can you forward the code. Please i need to Review this code. Pls Sir Raj. This is my Gmail Acc. Melvinbarrios08@gmail.com Thanks Sir.
ReplyDeleteChaitali Patil says true,
ReplyDeleteD:\received file.rar: The archive is either in unknown format or damaged
very nice sharing keep up sharing your great ideology. attractive work.
ReplyDeleteplease send code to my mail..it is very nice..
ReplyDeleteplease send code to my mail..sunil.trylogic@gmail.com...it is very nice blog
ReplyDeletecan u send me the source code to this email id gunanithinguna@gmail.com
ReplyDeletethank u bro . It work!
ReplyDeleteizmir
ReplyDeleteErzurum
Diyarbakır
TekirdaÄŸ
Ankara
GPUZH
EskiÅŸehir
ReplyDeleteAdana
Sivas
Kayseri
Samsun
AR2
https://titandijital.com.tr/
ReplyDeletesakarya parça eşya taşıma
aksaray parça eşya taşıma
urfa parça eşya taşıma
kocaeli parça eşya taşıma
ZXDS
bartın evden eve nakliyat
ReplyDeleteedirne evden eve nakliyat
mersin evden eve nakliyat
sinop evden eve nakliyat
siirt evden eve nakliyat
51L
B45D2
ReplyDeleteErzurum Parça Eşya Taşıma
Ağrı Evden Eve Nakliyat
Malatya Parça Eşya Taşıma
Tunceli Evden Eve Nakliyat
Batman Lojistik
1B42B
ReplyDeleteRize Parça Eşya Taşıma
Denizli Lojistik
Adana Parça Eşya Taşıma
Diyarbakır Şehirler Arası Nakliyat
İstanbul Parça Eşya Taşıma
Bitlis Lojistik
Ãœnye Marangoz
Çerkezköy Fayans Ustası
Btcturk Güvenilir mi
26387
ReplyDeleteArdahan Evden Eve Nakliyat
Bitcoin Nasıl Alınır
Mersin Şehirler Arası Nakliyat
Yalova Parça Eşya Taşıma
Poloniex Güvenilir mi
Bolu Evden Eve Nakliyat
Karaman Şehirler Arası Nakliyat
Bayburt Evden Eve Nakliyat
Iğdır Şehir İçi Nakliyat
4D5DE
ReplyDeleteBalıkesir Şehirler Arası Nakliyat
Anc Coin Hangi Borsada
Sakarya Şehirler Arası Nakliyat
Loop Network Coin Hangi Borsada
Çerkezköy Ekspertiz
NevÅŸehir Evden Eve Nakliyat
Maraş Parça Eşya Taşıma
Bitlis Şehir İçi Nakliyat
Kastamonu Şehirler Arası Nakliyat
B050A
ReplyDeleteYalova Evden Eve Nakliyat
Ãœnye Asma Tavan
Kilis Evden Eve Nakliyat
Trabzon Evden Eve Nakliyat
Niğde Şehir İçi Nakliyat
Çankaya Parke Ustası
Rize Parça Eşya Taşıma
Bonk Coin Hangi Borsada
Eskişehir Şehirler Arası Nakliyat
31F56
ReplyDeletereferanskodunedir.com.tr
FE12D
ReplyDeletetunceli canlı sohbet uygulamaları
bayburt canli sohbet chat
en iyi ücretsiz sohbet siteleri
ankara görüntülü canlı sohbet
yalova bedava sohbet uygulamaları
diyarbakır ücretsiz sohbet
aksaray en iyi görüntülü sohbet uygulaması
rastgele görüntülü sohbet uygulamaları
aydın mobil sohbet chat
5693E
ReplyDeletezonguldak nanytoo sohbet
sivas kadınlarla ücretsiz sohbet
sesli mobil sohbet
ankara kadınlarla sohbet et
ağrı telefonda kadınlarla sohbet
rize kadınlarla ücretsiz sohbet
bilecik ücretsiz sohbet odaları
rastgele görüntülü sohbet ücretsiz
ücretsiz sohbet sitesi
7303F
ReplyDeletebitlis telefonda rastgele sohbet
telefonda kadınlarla sohbet
nanytoo sohbet
afyon telefonda görüntülü sohbet
çorum canlı sohbet odaları
yabancı görüntülü sohbet
şırnak görüntülü sohbet kadınlarla
Adıyaman Ücretsiz Sohbet
ordu sohbet
0CE19
ReplyDeleteGate io Borsası Güvenilir mi
Bitcoin Para Kazanma
NWC Coin Hangi Borsada
Parasız Görüntülü Sohbet
Binance Referans Kodu
Binance Referans Kodu
Threads Yeniden Paylaş Satın Al
Likee App Beğeni Satın Al
Threads Takipçi Satın Al
AA3A7
ReplyDeleteCoin Nedir
Bitcoin Mining Nasıl Yapılır
Binance Referans Kodu
Okex Borsası Güvenilir mi
Flare Coin Hangi Borsada
Tiktok Ä°zlenme Hilesi
Görüntülü Sohbet Parasız
Azero Coin Hangi Borsada
Kripto Para Ãœretme