반응형
http://phantom00.tistory.com/14
뎃글에 대한 답글입니다. 간단히 만든 소스코드라 오류가 많을듯 합니다. 프로그램 닫을때 오류가 존재합니다.
서버
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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 SendImage
{
public partial class Form1 : Form
{
Socket sListener = null;
List<Socket> clientList = new List<Socket>();
Thread thread = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dia = new OpenFileDialog();
dia.Multiselect = false;
dia.Filter = "jpg files|*.jpg";
if (dia.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.ImageLocation = dia.FileName;
if (pictureBox1.InvokeRequired)
{
pictureBox1.BeginInvoke(new Action(() => this.pictureBox1.ImageLocation = dia.FileName));
}
else
{
this.pictureBox1.ImageLocation = dia.FileName;
}
foreach (var item in clientList)
{
Byte[] _data = ImageToByteArray(this.pictureBox1.Image);
item.Send(BitConverter.GetBytes(_data.Length));
item.Send(_data);
}
}
}
/// <summary>
/// 이미지를 바이트로
/// https://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public byte[] ImageToByteArray(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
private void Form1_Load(object sender, EventArgs e)
{
sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8081);
sListener.Bind(ipEndPoint);
sListener.Listen(20);
thread = new Thread(() => {
while (true)
{
Socket sClient = sListener.Accept();
IPEndPoint ip = (IPEndPoint)sClient.RemoteEndPoint;
Console.WriteLine("주소 {0}에서 접속", ip.Address);
clientList.Add(sClient);
}
});
thread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sListener.Close();
}
}
}
|
cs |
클라이언트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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 SendImage2
{
public partial class Form1 : Form
{
Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
List<Socket> clientList = new List<Socket>();
Thread thread = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
sClient.Connect(ipEndPoint);
thread = new Thread(() => {
while (true)
{
Byte[] _data = new byte[256];
sClient.Receive(_data);
int iLength = BitConverter.ToInt32(_data, 0);
Byte[] _data2 = new byte[iLength];
sClient.Receive(_data2);
if (pictureBox1.InvokeRequired)
{
pictureBox1.BeginInvoke(new Action(() => pictureBox1.Image = byteArrayToImage(_data2)));
}
else
{
pictureBox1.Image = byteArrayToImage(_data2);
}
}
});
thread.Start();
}
/// <summary>
/// 바이트를 image로
/// https://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv
/// </summary>
/// <param name="byteArrayIn"></param>
/// <returns></returns>
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(sClient != null) sClient.Close();
}
}
}
|
cs |
저도 소켓을 잘쓰는 편이 아니라 100프로 정답이라고 하긴 그렇치만 대충 이런식으로 전송하면 될꺼 같아서
소스 남겨드립니다.
클라이언트로 접속 후에 서버에서 파일하나씩 선택해보세요.
실행화면
'프로그래밍 > C#' 카테고리의 다른 글
[C#]AnimateWindow (0) | 2018.06.13 |
---|---|
[C#]폼을 투명하게 (0) | 2018.04.14 |
[C#]콤보박스 라인을 선택하는 컨트롤을 만들어보자. (0) | 2018.04.05 |
[C#]웹폰트를 윈도우폼에 적용해 보자 (1) | 2018.04.01 |
[C#]그라디에이션 (0) | 2018.03.18 |