我有同样的问题,请按照我的服务器解决方案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SERVER_WIN
{
public partial class Form1 : Form
{
//Declare and Initialize the IP Adress
static IPAddress ipAd = IPAddress.Parse("10.1.42.31");
//Declare and Initilize the Port Number;
static int PortNumber = 8888;
/* Initializes the Listener */
TcpListener ServerListener = new TcpListener(ipAd, PortNumber);
TcpClient clientSocket = default(TcpClient);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread ThreadingServer = new Thread(StartServer);
ThreadingServer.Start();
}
private void THREAD_MOD(string teste)
{
txtStatus.Text += Environment.NewLine + teste;
}
private void StartServer()
{
Action<string> DelegateTeste_ModifyText = THREAD_MOD;
ServerListener.Start();
Invoke(DelegateTeste_ModifyText, "Server waiting connections!");
clientSocket = ServerListener.AcceptTcpClient();
Invoke(DelegateTeste_ModifyText, "Server ready!");
while (true)
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[20];
networkStream.Read(bytesFrom, 0, 20);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
string serverResponse = "Received!";
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
}
catch
{
ServerListener.Stop();
ServerListener.Start();
Invoke(DelegateTeste_ModifyText, "Server waiting connections!");
clientSocket = ServerListener.AcceptTcpClient();
Invoke(DelegateTeste_ModifyText, "Server ready!");
}
}
}
}
}
您只需要在 Windows 窗体中放入一个名为 txtStatus 的控件 TextView。
客户计划:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace CLIENT_WIN
{
public partial class Form1 : Form
{
TcpClient tcpclnt = new TcpClient();
//Declare and Initialize the IP Adress
IPAddress ipAd = IPAddress.Parse("10.1.42.31");
//Declare and Initilize the Port Number;
int PortNumber = 8888;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Connecting.....");
try
{
tcpclnt.Connect(ipAd, PortNumber);
txtStatus.Text += Environment.NewLine + "Connected";
txtStatus.Text += Environment.NewLine + "Enter the string to be transmitted";
}
catch { }
}
private void btnEnviar_Click(object sender, EventArgs e)
{
String str = txtEnviar.Text + "$";
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
txtStatus.Text += Environment.NewLine + "Transmitting...";
//Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
string Response = Encoding.ASCII.GetString(bb);
txtStatus.Text += Environment.NewLine + "Response from server: " + Response;
}
}
}
对于客户,您需要进行一些控制;两个 TextView(txtStatus 和 txtEnviar)和一个名为 btnEnviar 的按钮,就是这样!
你必须首先运行服务器,然后才能运行客户端,如果你需要停止客户端,不要担心你可以让服务器运行。