C# 网络编程_socket实例
2012-11-08 21:41
525 查看
客户端:
//发送数据:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = new byte[1024];
Socket sk1, sk2;
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
sk1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sk1.Connect(remoteEP);
byte[] msg = Encoding.Unicode.GetBytes(textBox1.Text);
int bytesSent = sk1.Send(msg);
int bytesRec = sk1.Receive(bytes);//接收返回数据(可略)
this.textBox2.Text= Encoding.Unicode.GetString(bytes, 0, bytesRec);
}
catch(Exception p)
{
MessageBox.Show(p.ToString());
}
}
服务器端:
Thread th1;
th1 = new Thread(StartListening);
th1.Start();
public void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
this.label1.Text="Waiting for a connection";
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;
// An incoming connection needs to be processed.
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.Unicode.GetString(bytes, 0, bytesRec);
string dt = data;
this.textBox1.Text = dt;
// Show the data
//this.textBox1.Text = data;
// Echo the data back to the client.
byte[] msg = Encoding.Unicode.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
//发送数据:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = new byte[1024];
Socket sk1, sk2;
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
sk1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sk1.Connect(remoteEP);
byte[] msg = Encoding.Unicode.GetBytes(textBox1.Text);
int bytesSent = sk1.Send(msg);
int bytesRec = sk1.Receive(bytes);//接收返回数据(可略)
this.textBox2.Text= Encoding.Unicode.GetString(bytes, 0, bytesRec);
}
catch(Exception p)
{
MessageBox.Show(p.ToString());
}
}
服务器端:
Thread th1;
th1 = new Thread(StartListening);
th1.Start();
public void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
this.label1.Text="Waiting for a connection";
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;
// An incoming connection needs to be processed.
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.Unicode.GetString(bytes, 0, bytesRec);
string dt = data;
this.textBox1.Text = dt;
// Show the data
//this.textBox1.Text = data;
// Echo the data back to the client.
byte[] msg = Encoding.Unicode.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
相关文章推荐
- 网络编程C#篇(二):Socket面向连接简单实例
- C#网络编程之多线程socket实例
- C#网络编程(二)------多线程socket实例
- C# Socket网络编程实例
- 网络编程C#篇(二):Socket无连接简单实例 推荐
- C#网络编程(二)------多线程socket实例
- 网络编程之socket编程实例
- [转载]在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- C#2.0 Socket套接字编程之实例初探
- C#2.0 Socket套接字编程之实例初探
- C#网络程序设计(2)Socket基础编程
- C#Socket基本网络编程
- linux socket网络编程实例
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- 网络编程socket实例
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- C# Socket多线程编程实例
- C# Socket多线程编程实例
- python网络编程之TCP通信实例和socketserver框架
- [C#] Socket编程实例