日期:2014-05-18 浏览次数:20758 次
S核心代码
// 发送
private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
MyImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
myData= new Byte[1024];
myData = ms.ToArray();
MemoryStream ms2 = new MemoryStream();
ms2.Write(myData, 0, myData.Length);
Thread thread = new Thread(new ThreadStart(sendimage));
thread.Start();
}
// 图片发送
private void sendimage()
{
if (!isconnected)
{
IPAddress ip = IPAddress.Parse("192.168.0.2");
IPEndPoint endpoint = new IPEndPoint(ip, 9000);
mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mysocket.Connect(endpoint);
isconnected = true;
}
mysocket.Send(myData);
}
C核心代码
private void Form2_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(ReceiveImage));
thread.Start();
}
private void ReceiveImage()
{
listenser = new TcpListener(IPAddress.Parse("192.168.0.2"), 9000);
listenser.Start();
socket = listenser.AcceptSocket();
isconnected = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!isconnected)
{
return;
}
//MessageBox.Show("1");
MemoryStream mystream = new MemoryStream();
int number = 0;
// 1024*1024
byte[] imagebyte = new byte[1048576];
mystream.Position = 0;
number = socket.Receive(imagebyte);
if (number > 0)
{
mystream.Write(