using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SendFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public TcpListener listener;
private void button1_Click(object sender, EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.Serwer));
trd.Start();
}
public void Serwer()
{
listener = new TcpListener(IPAddress.Any, 1234);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
if (client.Connected)
Debug.Write("podłączony");
readFile(stream, client);
listener.Stop();
}
public void sendFile(NetworkStream s)
{
FileStream filestream = new FileStream(@"C:\Users\Piotr\Desktop\folderek\zdj.png", FileMode.Open);
byte[] data = new byte[filestream.Length];
filestream.Read(data, 0, (int)filestream.Length);
s.Write(data, 0, (int)filestream.Length);
//File.WriteAllBytes(@"C:\Users\Piotr\Desktop\folderek\zdjecie.png", data);
}
private void button2_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();
client.Connect(ip.Text, 1234);
NetworkStream stream = client.GetStream();
sendFile(stream);
stream.Close();
client.Close();
}
public void send(string text, NetworkStream s)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
s.Write(bytes, 0, bytes.Length);
/*byte[] bytes = BitConverter.GetBytes(number);
s.Write(bytes, 0, bytes.Length); */
}
public void read(NetworkStream s, TcpClient client)
{
byte[] readBytes = new byte[client.ReceiveBufferSize];
s.Read(readBytes, 0, Convert.ToInt32(client.ReceiveBufferSize));
string result = Encoding.UTF8.GetString(readBytes);
MessageBox.Show(result.ToString());
}
public void readFile(NetworkStream s, TcpClient client)
{
byte[] fileBytes = new byte[client.ReceiveBufferSize];
s.Read(fileBytes, 0, Convert.ToInt32(client.ReceiveBufferSize));
string path = @ścieżka.Text;
File.WriteAllBytes(path, fileBytes);
MessageBox.Show("Zapisano plik w: " + path);
}
private void Form1_Load(object sender, EventArgs e)
{
/*string text ="Jakis tekst";
byte[] bytes = Encoding.ASCII.GetBytes(text);
MessageBox.Show(bytes.Length.ToString());
byte[] bytes = BitConverter.GetBytes(101010);
MessageBox.Show(bytes.Length.ToString()); */
}
}
}