Created
August 8, 2022 17:10
-
-
Save naveenrobo/3ee8e2332b25768a9314327fdcaedf41 to your computer and use it in GitHub Desktop.
Send file from TCP Client to TCP Server in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net.Sockets; | |
namespace SlingshotTcpClient | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
TcpClient client = new TcpClient("127.0.0.1", 8000); | |
byte[] data = File.ReadAllBytes(@"C:\Users\navee\Downloads\DotworldLogo.png"); | |
byte[] dataLength = BitConverter.GetBytes(data.Length); | |
int bufferSize = 1024; | |
NetworkStream stream = client.GetStream(); | |
stream.Write(dataLength, 0, 4); | |
int bytesSent = 0; | |
int bytesLeft = data.Length; | |
while (bytesLeft > 0) | |
{ | |
int curDataSize = Math.Min(bufferSize, bytesLeft); | |
stream.Write(data, bytesSent, curDataSize); | |
bytesSent += curDataSize; | |
bytesLeft -= curDataSize; | |
} | |
stream.Close(); | |
client.Close(); | |
} | |
catch (ArgumentNullException e) | |
{ | |
Console.WriteLine("ArgumentNullException: {0}", e); | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException: {0}", e); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Net.Sockets; | |
namespace SlingshotTcpServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
TcpListener server = new TcpListener(System.Net.IPAddress.Any, 8000); | |
server.Start(); | |
while (true) | |
{ | |
Console.Write("Waiting for a connection... "); | |
// Perform a blocking call to accept requests. | |
// You could also use server.AcceptSocket() here. | |
TcpClient client = server.AcceptTcpClient(); | |
Console.WriteLine("Connected!"); | |
NetworkStream stream = client.GetStream(); | |
byte[] fileSizeBytes = new byte[4]; | |
int bytes = stream.Read(fileSizeBytes, 0, 4); | |
int dataLength = BitConverter.ToInt32(fileSizeBytes, 0); | |
int bytesLeft = dataLength; | |
byte[] data = new byte[dataLength]; | |
int bufferSize = 1024; | |
int bytesRead = 0; | |
while (bytesLeft > 0) | |
{ | |
int curDataSize = Math.Min(bufferSize, bytesLeft); | |
if (client.Available < curDataSize) | |
curDataSize = client.Available; //This saved me | |
bytes = stream.Read(data, bytesRead, curDataSize); | |
bytesRead += curDataSize; | |
bytesLeft -= curDataSize; | |
} | |
FileStream fs = new FileStream(@"D:\test.jpg", FileMode.OpenOrCreate); | |
fs.Write(data, 0, dataLength); | |
fs.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment