namespace test3{using System;
using System .Net .Sockets ;
using System .Net ;
using System .Text ;
using System .Threading ;
using System.Collections;
using System .IO ;
using UnityEngine; public class Socketfz
{
public static Socket soc =null ;
private static Thread nthread = null;
private static Thread thread = null;
private static byte[] buffer = new byte[1024];
public static string data ;
public static string data2;
public static string data3 ;
public static string data4 ;
public static string data5 ;
public Socketfz ()
{
} public void SocketConnection(string Lip ,int Lport){
IPAddress ip = IPAddress.Parse (Lip);
soc = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint (ip, Lport);
soc.Bind (ipep);
soc.Listen (10);
thread = new Thread (new ThreadStart (ListenC));
thread .Start ();
}
static void ListenC(){
while(true){
try{
Socket s = soc.Accept();
s.Send(Encoding.ASCII.GetBytes("success!"));
nthread = new Thread(Redata);
nthread.Start(s);
}catch(Exception e ){
MonoBehaviour .print (e.ToString());
}
}
}
static void Redata(object socket){
Socket socket1 = (Socket)socket; while (true) {
try{
int rs = socket1.Receive (buffer,buffer.Length ,0);
if(rs == 0){
continue ;
}
data = Encoding .ASCII .GetString (buffer,0,rs);
}catch (Exception e){
MonoBehaviour .print (e.ToString());
socket1 .Shutdown(SocketShutdown.Both);
socket1 .Close();
break;
}
}
}
}
}
当再有一个电脑接入的时候,我想再添加一个线程来处理它发送的数据。应该怎么添加啊????
using System .Net .Sockets ;
using System .Net ;
using System .Text ;
using System .Threading ;
using System.Collections;
using System .IO ;
using UnityEngine; public class Socketfz
{
public static Socket soc =null ;
private static Thread nthread = null;
private static Thread thread = null;
private static byte[] buffer = new byte[1024];
public static string data ;
public static string data2;
public static string data3 ;
public static string data4 ;
public static string data5 ;
public Socketfz ()
{
} public void SocketConnection(string Lip ,int Lport){
IPAddress ip = IPAddress.Parse (Lip);
soc = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint (ip, Lport);
soc.Bind (ipep);
soc.Listen (10);
thread = new Thread (new ThreadStart (ListenC));
thread .Start ();
}
static void ListenC(){
while(true){
try{
Socket s = soc.Accept();
s.Send(Encoding.ASCII.GetBytes("success!"));
nthread = new Thread(Redata);
nthread.Start(s);
}catch(Exception e ){
MonoBehaviour .print (e.ToString());
}
}
}
static void Redata(object socket){
Socket socket1 = (Socket)socket; while (true) {
try{
int rs = socket1.Receive (buffer,buffer.Length ,0);
if(rs == 0){
continue ;
}
data = Encoding .ASCII .GetString (buffer,0,rs);
}catch (Exception e){
MonoBehaviour .print (e.ToString());
socket1 .Shutdown(SocketShutdown.Both);
socket1 .Close();
break;
}
}
}
}
}
当再有一个电脑接入的时候,我想再添加一个线程来处理它发送的数据。应该怎么添加啊????
解决方案 »
- 【请教】关于多级联动菜单的WPF实现
- 保存变量的SQL语句怎么写啊?
- c#中datagridview的问题
- 如何让combobox只能够选择,不能够自己输入
- 100分!!难道报表一定要用代码拼吗?就不能图形界面画?
- C#中执行Insert出错的问题,请大家帮帮忙(急)!
- xml文件添加节点的问题??请各位高手帮忙解决一下
- 将一个从数据库SELECT * FROM TABLE出来的结果全部保存进Session然后如何从Session中取得 某个字段 的值?
- 如何根据汉字来检索拼音(只要首字母就行)-----------在线给分
- 请教关于IP地址筛选的问题
- 如何对datatable添加数据库已有数据行,而不影响DataAdapter.updata()
- 导入第三方dll后调用时出错
http://msdn.microsoft.com/zh-cn/library/vstudio/system.net.sockets.socket.beginaccept.aspx