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; using System.Net.Sockets; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Image img = Image.FromFile("nagato.jpg"); Socket mySocket; NetworkStream myStream; StreamWriter myWriter; StreamReader myReader; BinaryWriter myBWriter; BinaryReader myBReader; IPAddress myAddress; IPHostEntry myHostEntry; int myPort; IPEndPoint myEndPoint; private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = img; } private void button1_Click(object sender, EventArgs e) { int size; int count; mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); myPrint("ソケットを作成しました"); myHostEntry = Dns.Resolve("localhost"); myAddress = myHostEntry.AddressList[0]; myPort = 55555; myEndPoint = new IPEndPoint(myAddress, myPort); mySocket.Connect(myEndPoint); myPrint("接続完了"); myStream = new NetworkStream(mySocket); myWriter = new StreamWriter(myStream, Encoding.UTF8); myReader = new StreamReader(myStream, Encoding.UTF8); myBWriter = new BinaryWriter(myStream); myBReader = new BinaryReader(myStream); myWriter.AutoFlush = true; ImageConverter imgconv = new ImageConverter(); byte[] b = (byte[])imgconv.ConvertTo(img, typeof(byte[])); size = (int)b.Length; myWriter.WriteLine(size.ToString()); myPrint(size.ToString()); textBox1.Text = size.ToString(); for (count = 0; count < size; count++) { myBWriter.Write(b[count]); } mySocket.Close(); myPrint("ソケット切断"); } private void myPrint(string str) { listBox1.Items.Add(str); listBox1.TopIndex = listBox1.Items.Count - 1; listBox1.Refresh(); } } }