Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace FileSocketClient
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
- IPEndPoint ipEnd = new IPEndPoint(ipAddress[1], 5656);
- Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
- string fileName = "test.txt";
- string filePath = "c:/demo/";
- byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
- byte[] fileData = File.ReadAllBytes(filePath + fileName);
- byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
- byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
- fileNameLen.CopyTo(clientData, 0);
- fileNameByte.CopyTo(clientData, 4);
- fileData.CopyTo(clientData, 4 + fileNameByte.Length);
- clientSock.Connect(ipEnd);
- clientSock.Send(clientData);
- Console.WriteLine("File:{0} has been sent.", fileName);
- clientSock.Close();
- Console.ReadLine();
- }
- catch (Exception ex)
- {
- Console.WriteLine("File Sending fail." + ex.Message);
- Console.ReadLine();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment