Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
C#服务器端程序的编写
独一无二发起了问答2017-09-14
3
回复
378
浏览
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Socket_Test_01
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  //本地可以运行成功,服务端出错(服务器端的ip改成了服务器的ip)
            
            IPAddress ipaddress = IPAddress.Parse("192.168.1.113");

            EndPoint endpoint=new IPEndPoint(ipaddress,5000);
           
            tcpServer.Bind(endpoint);
            

            tcpServer.Listen(100);
           
            Console.WriteLine("服务器启动完成");

            Socket clientSocket= tcpServer.Accept();
            
            Console.WriteLine("接收到客户端的连接请求");

           
            string message= "Hello Client";
            var data=Encoding.UTF8.GetBytes(message);
           
            clientSocket.Send(data);
           
            Console.WriteLine("服务器给客户端发送消息:"+message);


            byte[] data2 = new byte[1024];
            
            int length = clientSocket.Receive(data2);
            
            string message2 = Encoding.UTF8.GetString(data2, 0, length);
           
            Console.WriteLine("服务器接收到客户端发来的消息:" + message2);

            Console.Read();
            Console.ReadKey();
        }
    }
}

 

所有回复
  • siki 2017-09-14

    启动有没有报错信息,看下报错信息

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 独一无二 2017-09-14


    服务器上没有详细的信息,只有这个,关键是本地一直都能正常运行,(局域网服务器也可以正常运行),在阿里云和腾讯云上都出现这样的错误

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2017-09-16

    服务器端的ip怎么填写的

    要通过ipconfig查询服务器端的ip

    可以学习下丛林战争 最后几个课时 服务器端的部署

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!