现在问题是多个客户端异步发送数据,服务端如何接收数据,我现在能实现功能是一个客户端发送数据,服务端就接收。客户端是一个TextBox,里面输入数据,点击发送。服务端就是一个Textbox。接收。但是出现第一个问题是:如何把客户端每次发来的数据都保存到记事本里。
下面是我的代码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;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace 接收
{
public partial class Form1 : Form
{
public static string data = null;
public Socket Client;
public Form1()
{
Form.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
Listen();
}
public Thread myThread;
public Socket newsock;
public IPEndPoint localEP;
public int localPort;
//用来设置服务端监听的端口号
public int setPort
{
get { return localPort; }
set { localPort = value; }
}
//监听函数
public void Listen()
{
//设置端口
setPort=6000;
//初始化SOCKET实例
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//初始化终结点实例
IPAddress ServerIP = IPAddress.Parse("128.1.2.251");
IPEndPoint localEP = new IPEndPoint(ServerIP, 6000);
try
{
//绑定
newsock.Bind(localEP);
//监听
newsock.Listen(10);
//用于设置按钮状态
m_Listening = true;
newsock.BeginAccept(new AsyncCallback(OnConnectRequest), newsock);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
//当有客户端连接时的处理
public void OnConnectRequest(IAsyncResult ar)
{
//初始化一个SOCKET,用于其它客户端的连接
Socket server1 = (Socket)ar.AsyncState;
Client = server1.EndAccept(ar);
//将要发送给连接上来的客户端的提示字符串
string strDateLine = "Welcome here";
Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine);
//等待新的客户端连接
server1.BeginAccept(new AsyncCallback(OnConnectRequest), server1);
int recv = Client.Receive(byteDateLine);
string stringdata = Encoding.ASCII.GetString(byteDateLine, 0, recv);
DateTimeOffset now = DateTimeOffset.Now;
//获取客户端的IP和端口
string ip = Client.RemoteEndPoint.ToString();
if (stringdata == "STOP")
{
//当客户端终止连接时
Console .WriteLine(ip+"已从服务器断开");
}
//显示客户端发送过来的信息
this.textBox1 .Text = stringdata ;
//目前我是用this.textbox1来接收的。但是,客户端变数据,服务端就清空也变数据,没法保存上次传过来的数据。
}
//窗口关闭时中止线程。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myThread != null)
{
myThread.Abort();
}
}
}
}
下面是我的代码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;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace 接收
{
public partial class Form1 : Form
{
public static string data = null;
public Socket Client;
public Form1()
{
Form.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
Listen();
}
public Thread myThread;
public Socket newsock;
public IPEndPoint localEP;
public int localPort;
//用来设置服务端监听的端口号
public int setPort
{
get { return localPort; }
set { localPort = value; }
}
//监听函数
public void Listen()
{
//设置端口
setPort=6000;
//初始化SOCKET实例
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//初始化终结点实例
IPAddress ServerIP = IPAddress.Parse("128.1.2.251");
IPEndPoint localEP = new IPEndPoint(ServerIP, 6000);
try
{
//绑定
newsock.Bind(localEP);
//监听
newsock.Listen(10);
//用于设置按钮状态
m_Listening = true;
newsock.BeginAccept(new AsyncCallback(OnConnectRequest), newsock);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} }
//当有客户端连接时的处理
public void OnConnectRequest(IAsyncResult ar)
{
//初始化一个SOCKET,用于其它客户端的连接
Socket server1 = (Socket)ar.AsyncState;
Client = server1.EndAccept(ar);
//将要发送给连接上来的客户端的提示字符串
string strDateLine = "Welcome here";
Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine);
//等待新的客户端连接
server1.BeginAccept(new AsyncCallback(OnConnectRequest), server1);
int recv = Client.Receive(byteDateLine);
string stringdata = Encoding.ASCII.GetString(byteDateLine, 0, recv);
DateTimeOffset now = DateTimeOffset.Now;
//获取客户端的IP和端口
string ip = Client.RemoteEndPoint.ToString();
if (stringdata == "STOP")
{
//当客户端终止连接时
Console .WriteLine(ip+"已从服务器断开");
}
//显示客户端发送过来的信息
this.textBox1 .Text = stringdata ;
//目前我是用this.textbox1来接收的。但是,客户端变数据,服务端就清空也变数据,没法保存上次传过来的数据。
}
//窗口关闭时中止线程。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myThread != null)
{
myThread.Abort();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货