2 weixin 37269903 weixin_37269903 于 2017.01.06 15:08 提问

如何将服务器端 客服端 控制台程序转成窗体应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace 客户端
{
class Program
{
private static byte[] result = new Byte[1024];

    static void Main(string[] args)
    {
        IPAddress ip = IPAddress.Parse("127.0.0.1");
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            clientSocket.Connect(new IPEndPoint(ip, 8012));
            Console.WriteLine("连接服务器成功");
        }
        catch
        {
            Console.WriteLine("连接服务器失败,请按回车键退出");
            return;

        }
        int receiveLength = clientSocket.Receive(result);
        Console.WriteLine("接收服务器:{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
        string sendMessage = "client send Message Hello";
        clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));
        Console.WriteLine("向服务器发送消息:{0}", sendMessage);
        clientSocket.Shutdown(SocketShutdown.Both);
        clientSocket.Close();
        Console.ReadLine();

    }
}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace 服务器端
{
class Program
{
private static byte[] result = new Byte[1024];
private static int myport = 8012;
static Socket serverSocket;
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, myport));
serverSocket.Listen(10);
Console.WriteLine("启动监听{0}", serverSocket.LocalEndPoint.ToString());
string sendMessage = "sever send Message Hello";
Socket clientSocket = serverSocket.Accept();
clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));
Console.WriteLine("向客户端发送信息:{0}", sendMessage);
int receiveNumber = clientSocket.Receive(result);
Console.WriteLine("接收客户端{0}消息{1}", clientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
Console.ReadLine();

    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!