PHP写的服务器端,C#写的客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace UpdClient
{
class Program
{
static void Main(string[] args)
{
//设定服务器IP地址
IPAddress ip = IPAddress.Parse("127.0.0.1");
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, 2049)); //配置服务器IP与端口
Console.WriteLine("连接服务器成功");
}
catch
{
Console.WriteLine("连接服务器失败,请按回车键退出!");
return;
}
string welcome = "watch";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
clientSocket.SendTo(data, data.Length, SocketFlags.None, new IPEndPoint(ip, 2048));
//通过clientSocket接收数据
byte[] result = new byte[1024];
int receiveLength = clientSocket.Receive(result);
Console.WriteLine("接收服务器消息:{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
}
}
}