程序连接到localhost上的服务器,但不能通过LAN或Internet工作

时间:2016-01-01 17:38:53

标签: c# sockets asynchronous

我使用Windows Forms在普通C#上制作了一个超级简单的游戏来学习一些关于让程序通过互联网“对话”的方法。服务器和客户端在相同的程序上,用户只需通过单击按钮选择他是哪一个。

只要服务器和客户端都使用ip 127.0.0.1连接在同一台PC上运行,它目前正常工作。如果我尝试使用内部或外部ip(端口打开)它不再起作用(“接受之前”触发,但“接收之前”没有)。

我得到了Darryl Braaten的代码的基础,他回答了别人的问题: create c# asynchronous socket client in multithreaded environment

AsyncServer.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace blockbattle
{
    public partial class AsyncClient
    {
        private const int port = 11000;
        private readonly int _clientId;
        private readonly Random _random;
        public string clientPos = "";
        public string serverPos = "";
        public string newBullets = "";
        public bool bulletsSent = false;
        public string receivedBullets = "";
        public string lastBullet = "";

        public AsyncClient(int clientId)
        {
            _clientId = clientId;
            _random = new Random(clientId);
        }

        public void StartClient(IPAddress serverIP)
        {
            try
            {
                Socket workSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ClientState state = new ClientState { WorkSocket = workSocket };
                workSocket.BeginConnect(new IPEndPoint(serverIP, port), ConnectCallBack, state);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        private void ConnectCallBack(IAsyncResult ar)
        {
            ClientState state = (ClientState)ar.AsyncState;
            state.WorkSocket.EndConnect(ar);
            Send(state);
        }

        private void Receive(ClientState clientState)
        {
            clientState.WorkSocket.BeginReceive(clientState.Buffer, 0, ClientState.BufferSize, 0, ReceiveCallBack, clientState);
        }

        private void ReceiveCallBack(IAsyncResult ar)
        {
            ClientState state = (ClientState)ar.AsyncState;
            Socket client = state.WorkSocket;
            int byteReceived = client.EndReceive(ar);
            if (byteReceived > 0)
            {
                string receivedString = Encoding.UTF8.GetString(state.Buffer, 0, byteReceived);
                string[] receivedData = receivedString.Split('+');

                serverPos = receivedData[0];
                if (receivedData[1].Length > 0)
                    receivedBullets = receivedData[1];

                Array.Clear(state.Buffer, 0, state.Buffer.Length);
                Thread.Sleep(33);
                Send(state);
            }
        }

        private void Send(ClientState clientState)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(string.Format("{0}+{1}+{2}", _clientId, clientPos, newBullets));
            if (newBullets.Length > 0)
            {
                newBullets = "";
                bulletsSent = true;
            }

            try
            {
                clientState.WorkSocket.BeginSend(buffer, 0, buffer.Length, 0, BeginSendCallBack, clientState);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        private void BeginSendCallBack(IAsyncResult ar)
        {
            ClientState state = (ClientState)ar.AsyncState;
            int byteSent = state.WorkSocket.EndSend(ar);
            Receive(state);
        }
    }

    public class ClientState
    {
        // Client socket.
        public Socket WorkSocket = null;
        // Size of receive buffer.
        public const int BufferSize = 1024;
        // Receive buffer.
        public byte[] Buffer = new byte[BufferSize];

        public int Count = 0;
    }
}

AsyncClient.cs

'aoColumns': [
        {'bVisible':true}, // 0 full name
        {'bVisible':false}, // 1 first name
        {'bVisible':false}, // 2 last name
        {'bVisible':false}, // 3 title
        {'bVisible':true}, // 4 company
        {'bVisible':true}, // 5 email
        {'bVisible':true}, // 6 phone
        {'bVisible':false}, // 7 fax
        {'bVisible':false}, // 8 address1
        {'bVisible':false}, // 9 address2
        {'bVisible':false}, // 10 city
        {'bVisible':false}, // 11 state
        {'bVisible':false}, // 12 postal code
        {'bVisible':false}, // 13 country
        {'bVisible':false}, // 14 website
        {'bVisible':false}, // 15 facebook
        {'bVisible':false}, // 16 google+
        {'bVisible':false}, // 17 twitter
        {'bVisible':false}, // 18 linkedin
        {'bVisible':true}, // 19 source
        {'bVisible':true}, // 20 status
        {'bVisible':true}, // 21 type
        {'bVisible':false}, // 22 tags
        {'bVisible':false}, // 23 summary
        {'bVisible':false}, // 24 creation date
        {'bVisible':false}, // 25 last update date -- sort on this column
    ],

1 个答案:

答案 0 :(得分:1)

subset(df, a %in% c(2,3)) # a b # 4 2 4 # 5 2 5 # 6 2 6 # 7 3 7 # 8 3 8 # 9 3 9 中的IPAddress.Loopback更改为IPAddress.Any