接收TCP连接问题 [英] receiving TCP connection issues

查看:161
本文介绍了接收TCP连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地设定,让我团结的应用程序可以在TCP数据。然而,当我运行该项目整个应用程序,并冻结它已经收到了消息,直到什么都不会做。然后,一旦消息已被接收,它打印的信息,并不会让我的客户再次连接到它。



这代码是我的TCP阅读课的全部,是有一些愚蠢的我在这里做,是造成这个?



有没有更好的办法在TCP数据读取?



 使用UnityEngine; System.Collections中使用
;使用的System.Net.Sockets
;使用System.Net
;
使用System.Text;
使用系统;
:使用System.IO;

公共类TCPConnection:MonoBehaviour
{
公共字符串IP_ADDRESS =;
公众诠释端口号= 0;

无效的start()
{


{
ip地址ip地址= IPAddress.Parse(IP地址);
的TcpListener监听器=新的TcpListener(ip地址,端口号);
listener.Start();
打印(服务器在端口上运行+端口号);
打印(当地的终点是:+ listener.LocalEndpoint);
打印(正在等待连接......);

Socket套接字= listener.AcceptSocket();
打印(+ socket.RemoteEndPoint连接从接受);

的byte [] B =新的字节[100];
INT K = socket.Receive(B);
打印(收到......);
的for(int i = 0; I< k;我++)
打印(Convert.ToChar(B [I]));

ASCIIEncoding ASCII =新ASCIIEncoding();
socket.Send(ascii.GetBytes(以下简称字符串被服务器收到));
打印(\\\
Sent ACK);

}

赶上(例外五)
{
打印(错误......+ e.Message);
}

}
}


解决方案

listener.AcceptSocket socket.Receive 是一个阻塞调用。所以这是正常的,你的UI冻结。再次,你已经接受了第一个连接后,你永远不叫 listener.AcceptSocke 来获得一个新的集合。



使用这些方法是使用异步调用,线程或者任务的常用方式。



修改



一个例子:

 无效的start()
{
Task.Factory.StartNew( ()=>
{
的TcpListener监听器=新的TcpListener(IPAddress.Any,12345);
listener.Start();

,而(真)
{
Socket套接字= listener.AcceptSocket();
Task.Factory.StartNew(()=>
{
的byte [] b =新字节[100 ];
INT K = socket.Receive(二);
});
}
});
}


I've managed to set it up so my unity app can take in tcp data. However when I run the project the entire application freezes and won't do anything until it has received a message. Then, once a message has been received, it prints that message out and won't allow my client to connect to it again.

This code is the entirety of my TCP reading class, is there something silly I'm doing here that is causing this?

Is there a better way to read in TCP data?

using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System;
using System.IO;

public class TCPConnection: MonoBehaviour 
{
    public string ip_address = "";
    public int port_number = 0;

     void Start()
    {

        try
        {
            IPAddress ipAddress = IPAddress.Parse(ip_address);
            TcpListener listener = new TcpListener(ipAddress, port_number);
            listener.Start();
            print("The server is running at port " + port_number);
            print("The local end point is :" + listener.LocalEndpoint);
            print("Waiting for connection.....");

            Socket socket = listener.AcceptSocket();
            print("Connection accepted from " + socket.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = socket.Receive(b);
            print("recieved...");
            for (int i = 0; i < k; i++)
                print (Convert.ToChar(b[i]));

            ASCIIEncoding ascii = new ASCIIEncoding();
            socket.Send(ascii.GetBytes("The string was recieved by the server"));
            print("\nSent ack");

        }

        catch (Exception e)
        {
            print("error...." + e.Message);
        }

    }
}

解决方案

listener.AcceptSocket and socket.Receive are a blocking calls. So it is normal that your UI freezes. And after you have accepted the first connection, you never call listener.AcceptSocke again to get a new collection.

The common way to use these methods are using async calls, threads or Tasks.

EDIT

An example:

void Start()
{
    Task.Factory.StartNew(() =>
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 12345);
            listener.Start();

            while (true)
            {
                Socket socket = listener.AcceptSocket();
                Task.Factory.StartNew(() =>
                {
                    byte[] b = new byte[100];
                    int k = socket.Receive(b);
                });
            }
        });
}

这篇关于接收TCP连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆