 |
|
//นี่คื่อส่วนที่ทำการส่ง
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net; // For Using Network Programming Classes
using System.Net.Sockets; // For Using Socket Classes
using System.IO; // For Input Output Streams Classes
using System.Threading; // For Multi Threading In the Same App
namespace send1
{
public partial class Form1 : Form
{
int groupPort = 6666;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("คลิกส่งละนะ");
send();
}
void send()
{
MessageBox.Show("เริ่ม");
Socket client_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), groupPort);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
FileStream fs = new FileStream(@"C:\test.png", FileMode.Open, FileAccess.Read);
BinaryReader binFile = new BinaryReader(fs);
byte[] downBuffer = new byte[1024000];
//byte[] downBuffer = Encoding.ASCII.GetBytes(binFile);
int bytesSize = 0;
while ((bytesSize = binFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
try
{
byte[] buffer = BitConverter.GetBytes(downBuffer.Length);
socket.SendTo(buffer, groupEP);
MessageBox.Show("ส่งภาพได้แล้วจ้า");
}
catch
{
MessageBox.Show("ยังส่งไมได้เลยน๊ะ");
}
}
}
}
}
//มันบอกว่าส่งได้แล้ว
//นี่คือส่วนการรับที่ยังรับมั้ยได้...ช่วยดูและเรียบเรียงให้หน่อยนะคะ
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net; // For Using Network Programming Classes
using System.Net.Sockets; // For Using Socket Classes
using System.IO; // For Input Output Streams Classes
using System.Threading; // For Multi Threading In the Same App
namespace receive1
{
public partial class Form1 : Form
{
byte[] buffer = new byte[1024000];
//string stringData1;
Socket sock1;
IPEndPoint iep1;
EndPoint ep1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
sock1 = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
iep1 = new IPEndPoint(IPAddress.Any, 6666);
sock1.Bind(iep1);
ep1 = (EndPoint)iep1;
CallData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error1 Load",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CallData()
{
sock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ep1, new AsyncCallback(revImage), ep1);
}
private void revImage(IAsyncResult ar)
{
try
{
// stringData1 = Encoding.UTF8.GetString(buffer);
int downbuffer = BitConverter.ToInt32(buffer, 0);
sock1.EndReceiveFrom(ar, ref ep1);
while (true)
{
//byte[] downbuffer =
MemoryStream ms = new MemoryStream(downbuffer);
Image bmp = Image.FromStream(ms);
pictureBox1.Image = bmp;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error OnReceive!!");
}
}
}
}
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2009-09-04 21:24:01 |
By :
mikky |
View :
3786 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |