---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
客户端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ExaClient
{
public partial class Client : Form
{
public Client()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
Socket socketClient = null;//创建客户端套接字
Thread threadClient = null;//创建客户端接收数据的线程
private void btnClient_Click(object sender, EventArgs e)
{
IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
IPEndPoint endport = new IPEndPoint(address, int.Parse(txtport.Text.Trim()));
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketClient.Connect(endport);//连接服务器
threadClient = new Thread(RecMsg);
threadClient.IsBackground = true;
threadClient.Start();
}
void RecMsg()//接收数据的方法
{
while (true)
{
//准备一个空间接收其数据(2M的字节数组)
byte[] recArr = new byte[1024 * 1024 * 2];
//取得接收到的数据长度
int length = socketClient.Receive(recArr);
//将接收到的字节数组转换为字符串
string recData = System.Text.Encoding.UTF8.GetString(recArr, 0, length);
showMsg(recData);//通过调用showMsg方法将数据将字符串显示到文本框中
}
}
void showMsg(string msg)
{
txtMsg.AppendText(msg + "\n");
}
}
}
服务器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ExamServer
{
public partial class serverSoket : Form
{
public serverSoket()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
Thread threadLook = null;//负责监听客户端连接请求的线程;
Socket lookSocket = null;//创建服务器端负责监听的套接字
private void btnServer_Click(object sender, EventArgs e)
{ //参数(使用IPV4寻址协议,使用流式链接,使用TCP协议传输数据)
lookSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//获得文本框的IP地址对象
IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
//创建包含IP和port的网络节点对象
IPEndPoint endpoint = new IPEndPoint(address,int.Parse(txtport.Text.Trim()));
//将负责套监听的 套接字绑定 唯一的ip和端口上
lookSocket.Bind(endpoint);
//设置监听对象的长度
lookSocket.Listen(10);
//创建负责监听的线程,并传入监听方法
threadLook=new Thread(lookconnting);
threadLook.IsBackground=true; //设置为后台线程
threadLook.Start(); //开启线程
showMsg("服务器启动监听成功~~~");
}
Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
void lookconnting()
{
while (true)//持续不断的监听客户端的请求
{
Socket sokConnetion = lookSocket.Accept();
//向列表中添加客户端的ip端口字符串,作为客户端各自的标识
listOnline.Items.Add(sokConnetion.RemoteEndPoint.ToString());
//将与客户端套通信的套接字对象添加到键值对集合中,
dict.Add(sokConnetion.RemoteEndPoint.ToString(),sokConnetion);
showMsg("客户端链接成功"+sokConnetion.RemoteEndPoint.ToString());
}
}
void showMsg(string msg)
{
txtMsg.AppendText(msg+"\n");
}
private void btnSendMsg_Click(object sender, EventArgs e)
{
string sendData = txtSendMsg.Text.Trim();
//将要发送出去的字符串转换为字节数组
byte[] sendArr = System.Text.Encoding.UTF8.GetBytes(sendData);
string key = listOnline.Text;//取出在列表listOnline选中的客户端
dict[key].Send(sendArr);//根据选中的key调用字典集合中对应的套接字的send方法,发送数据
// sokConnetion.Send(sendArr);//发送数据
showMsg("发送数据:"+sendData);
}
}
}
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://net.itheima.com/