socket编程一
//提供网际协议(IP)地址IPAddress
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
//将网络端点表示为IP地址和端口号
IPEndPoint point;
//定义两个套接字
Socket socket;
Socket acceptedSocket;
//开启接受套接字的服务
private void button1_Click(object sender, EventArgs e)
{
try
{
point = new IPEndPoint(HostIP, Int32.Parse("1571"));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, Prot
准备弄个.net的Socket系列,先弄个简单的
客户端代码
Code
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class net_client : Form
{
public net_client()
{
InitializeComponent();
}
private void btn_send_Click(object sender, EventArgs e)
{
Socket socket;
//提供网际协议的ip地址
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
//将网络端点表示为ip地址和端口号
IPEndPoint point;
try
{
point = new IPEndPoint(HostIP, Int32.Parse("1571"));
//AddressFamily指定Socket的寻址方式
//指定Socket的协议类型ProtocolType.Tcp
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(point);
//向服务器发送数据
byte[] sendByte = new byte[64];
string sendStr = this.textBox1.Text + "\r\n";
sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
socket.Send(sendByte, sendByte.Length, 0);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class net_client : Form
{
public net_client()
{
InitializeComponent();
}
private void btn_send_Click(object sender, EventArgs e)
{
Socket socket;
//提供网际协议的ip地址
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
//将网络端点表示为ip地址和端口号
IPEndPoint point;
try
{
point = new IPEndPoint(HostIP, Int32.Parse("1571"));
//AddressFamily指定Socket的寻址方式
//指定Socket的协议类型ProtocolType.Tcp
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(point);
//向服务器发送数据
byte[] sendByte = new byte[64];
string sendStr = this.textBox1.Text + "\r\n";
sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
socket.Send(sendByte, sendByte.Length, 0);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
服务器端的代码
Code
using System;
using System.Text;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Net.Sockets;
namespace WindowsApplication1
{
public partial class net_server : Form
{
public net_server()
{
InitializeComponent();
}
//声明委托
delegate void SetTextCallback(string text);
private void SetText(string text)
{
textBox1.AppendText(text + "\r\n");
}
//提供网际协议(IP)地址IPAddress
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
//将网络端点表示为IP地址和端口号
IPEndPoint point;
//定义两个套接字
Socket socket;
Socket acceptedSocket;
//开启接受套接字的服务
private void button1_Click(object sender, EventArgs e)
{
try
{
point = new IPEndPoint(HostIP, Int32.Parse("1571"));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(point);
socket.Listen(50);
//acceptedSocket = socket.Accept();
Thread thread = new Thread(new ThreadStart(Process));
thread.Start();
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
}
//要专门用一个线程来accept 一个线程来receive数据
private void Process()
{
acceptedSocket = socket.Accept();
if (acceptedSocket.Connected)
{
byte[] receiveByte = new byte[64];
acceptedSocket.Receive(receiveByte, receiveByte.Length, 0);
string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte);
//在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定的委托
this.Invoke(new SetTextCallback(SetText), new object[] { strInfo });
}
else
{
this.textBox1.Text = "还没有信息";
}
}
}
}
using System;
using System.Text;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Net.Sockets;
namespace WindowsApplication1
{
public partial class net_server : Form
{
public net_server()
{
InitializeComponent();
}
//声明委托
delegate void SetTextCallback(string text);
private void SetText(string text)
{
textBox1.AppendText(text + "\r\n");
}
//提供网际协议(IP)地址IPAddress
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
//将网络端点表示为IP地址和端口号
IPEndPoint point;
//定义两个套接字
Socket socket;
Socket acceptedSocket;
//开启接受套接字的服务
private void button1_Click(object sender, EventArgs e)
{
try
{
point = new IPEndPoint(HostIP, Int32.Parse("1571"));
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(point);
socket.Listen(50);
//acceptedSocket = socket.Accept();
Thread thread = new Thread(new ThreadStart(Process));
thread.Start();
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
}
//要专门用一个线程来accept 一个线程来receive数据
private void Process()
{
acceptedSocket = socket.Accept();
if (acceptedSocket.Connected)
{
byte[] receiveByte = new byte[64];
acceptedSocket.Receive(receiveByte, receiveByte.Length, 0);
string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte);
//在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定的委托
this.Invoke(new SetTextCallback(SetText), new object[] { strInfo });
}
else
{
this.textBox1.Text = "还没有信息";
}
}
}
}