How to C# Socket Programming using Visual Studio.Net

Learn step by step network programming using C#.Net socket, remoting, WCF and SignalR technology from lots of source code and description. All source code download link available at end of the blog post.

File Transfer using C# .Net Socket Programming 3/3



Continued code...
Server Code:
[C#]

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace beginSocketServer
{
//FILE TRANSFER USING C#.NET SOCKET PROGRAMMING - SERVER
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("That program can transfer small file. I've test up to 850kb file");
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(100);
Socket clientSock = sock.Accept();

byte[] clientData = new byte[1024 * 5000];
string receivedPath = "e:/";

int receivedBytesLen = clientSock.Receive(clientData);
int fileNameLen = BitConverter.ToInt32(clientData, 0);
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);

bWrite.Close();
clientSock.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("File Receiving fail." + ex.Message);
}
}
}
}

[Visual Basic]

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Namespace beginSocketServer
'FILE TRANSFER USING C#.NET SOCKET PROGRAMMING- SERVER
Class Program
Private Shared Sub Main(ByVal args As String())
Try
Console.WriteLine("That program can transfer small file. I've test up to 850kb file")
Dim ipEnd As New IPEndPoint(IPAddress.Any, 5656)
Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
sock.Bind(ipEnd)
sock.Listen(100)
Dim clientSock As Socket = sock.Accept()

Dim clientData As Byte() = New Byte(1024 5000 - 1) {}
Dim receivedPath As String = "e:/"

Dim receivedBytesLen As Integer = clientSock.Receive(clientData)

Dim fileNameLen As Integer = BitConverter.ToInt32(clientData, 0)
Dim fileName As String = Encoding.ASCII.GetString(clientData, 4, fileNameLen)

Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName)

Dim bWrite As New BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append))


bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen)

Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath)

bWrite.Close()
clientSock.Close()
Console.ReadLine()
Catch ex As Exception
Console.WriteLine("File Receiving fail." & ex.Message)
End Try
End Sub
End Class
End Namespace

Labels: , , , , , , , , , , , , ,

leave a response