wadewu123 2016-07-27 05:33 采纳率: 0%
浏览 1867

怎样用C#通过socket发送数组,代码如下,求大神指正

客户端
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System .Xml .Serialization ;
using System .IO ;
using System .Runtime .Serialization ;
using System .Runtime .Serialization .Formatters .Binary ;
using System.Net;

using System.Net.Sockets;

public class wade2 : MonoBehaviour {

// Use this for initialization
public int[] arr;
public ArrayList list ;

public byte[] array = null;
void Start () {
    int[] arr = new int[] { 1,2,3,4};

    ArrayList list = new ArrayList (arr);
}


public void Cli(){
    IPAddress ip = IPAddress.Parse("192.168.1.104");
    Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    try
    {
        clientSocket.Connect(new IPEndPoint(ip,8000)); //配置服务器IP与端口   
        Debug.Log ("连接服务器成功");
    }
    catch(Exception ex)
    {
        Debug.Log ("客户端连接异常:"+ex.Message);
        return;
    }


    try
    {
        Stream stream = new MemoryStream();
        //定义一个格式化器
        BinaryFormatter bf = new BinaryFormatter();
        //将Arraylist中的对象逐一进行序列化
        foreach (object obj in list)
        {
            bf.Serialize(stream, obj);
        }
        array = new byte[stream.Length];
        //将二进制流写入数组
        stream.Position = 0;
        stream.Read(array, 0, (int)stream.Length);
        //关闭流

        //string sendMessage = " helloworld";
        clientSocket.Send(array,array.Length,0);
        Debug.Log ("向服务器发送消息:"+list[1]);
        stream.Close();
        //receiveLength = clientSocket.Receive(result);
        //Console.WriteLine("接收服务器消息:{0}", Encoding.ASCII.GetString(result, 0, receiveLength));                    
    }
    catch(Exception ex)
    {    
        Debug.Log ("发送有问题:"+ex.Message);
        clientSocket.Shutdown(SocketShutdown.Both);
        clientSocket.Close();

    }

}
// Update is called once per frame
void Update () {

}

}

服务端
using UnityEngine;

using System.Collections;

using System;
using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Threading;

using System .IO ;
using System.Xml.Serialization ;
using System .Runtime .Serialization ;
using System .Runtime .Serialization .Formatters .Binary ;

public class james2 : MonoBehaviour {

// Use this for initialization
public byte[] result = new byte[3072];
public int myProt = 8000;   //端口   
public Socket serverSocket;
public void Ser()
{
    //服务器IP地址   
    IPAddress ip = IPAddress.Parse("192.168.1.104");
    serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    serverSocket.Bind(new IPEndPoint(ip, myProt));  //绑定IP地址:端口   
    serverSocket.Listen(10);    //设定最多10个排队连接请求   
    Debug.Log ("启动监听成功"+serverSocket.LocalEndPoint.ToString());
    //通过Clientsoket发送数据   
    Thread myThread = new Thread(ListenClientConnect);
    myThread.Start();

}
public  void ListenClientConnect()  
{  
    while (true)  
    {  
        Socket clientSocket = serverSocket.Accept();
        //clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));  
        Thread receiveThread = new Thread(ReceiveMessage);  
        receiveThread.Start(clientSocket);  
    }  
}  
public  void ReceiveMessage(object clientSocket)  
{  
    Socket myClientSocket = (Socket)clientSocket;
    while (true)  
    {  
        try  
        {  
            //通过clientSocket接收数据   
            int receiveNumber = myClientSocket.Receive(result);  
            if (receiveNumber == 0)//连接断开,或者在TCPClient上调用了Close()方法,或者在流上调用了Dispose()方法。  
            {  
                Debug.Log("没有信息过来");  
                break;  
            } 
            //Debug.Log ("接收客户端{0}消息{1}"+ myClientSocket.RemoteEndPoint.ToString()+ Encoding.ASCII.GetString(result, 0, receiveNumber));
            MemoryStream stream = new MemoryStream(result);
            //定义一个格式化器
            BinaryFormatter bf = new BinaryFormatter();
            ArrayList list = new ArrayList();
            while (stream.Position != stream.Length)
            {
                list.Add(bf.Deserialize(stream));
            }
            Debug.Log ("X"+list[1]+"Y"+list[2]+"Z"+list[3]);
            stream.Close();
        }  
        catch(Exception ex)  
        {  
            Debug.Log(ex.Message);  
            myClientSocket.Shutdown(SocketShutdown.Both);  
            myClientSocket.Close();  
            break;  
        }  
    }
} 
void Start () {

}

// Update is called once per frame
void Update () {

}

}

  • 写回答

1条回答 默认 最新

  • wadewu123 2016-07-27 05:36
    关注

    在unity中运行后,显示信息发不过去,求助!!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)