本文介绍了如何建立TCP客户端服务器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我创建了客户端(Windows)和服务器(控制台......并尝试在这两者之间建立连接并从客户端向服务器发送消息。 以下是错误:I created Client(Windows) and Server(Console...and trying to make connection between these two and send message from client to server.The following is the Error:InvalidOperationException was unhandled.An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll 发生了,发生了System =InvalidOperationException' 我尝试了什么: 服务器:What I have tried:Server:static void Main(string[] args) { TcpListener serverSocket = new TcpListener(8281); int requestCount = 0; TcpClient clientSocket = default(TcpClient); serverSocket.Start(); Console.WriteLine(" >> Server Started"); clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine(" >> Accept connection from client"); requestCount = 0; while ((true)) { try { requestCount = requestCount + 1; NetworkStream networkStream = clientSocket.GetStream(); byte[] bytesFrom = new byte[10025]; networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize); string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); Console.WriteLine(" >> Data from client - " + dataFromClient); string serverResponse = "Last Message from client" + dataFromClient; Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); Console.WriteLine(" >> " + serverResponse); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } clientSocket.Close(); serverSocket.Stop(); Console.WriteLine(" >> exit"); Console.ReadLine(); } 客户:Client: public partial class Form1 : Form { System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { msg("Client Started"); clientSocket.Connect("127.0.0.1", 8281); label1.Text = "Client Socket Program - Server Connected ..."; } public void msg(string mesg) { textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg; } private void button1_Click_1(object sender, EventArgs e) { NetworkStream serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); byte[] inStream = new byte[10025]; serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize); string returndata = System.Text.Encoding.ASCII.GetString(inStream); msg(returndata); textBox2.Text = ""; textBox2.Focus(); }}推荐答案 客户:Client: public partial class Form1 : Form { System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { msg("Client Started"); clientSocket.Connect("127.0.0.1", 8281); label1.Text = "Client Socket Program - Server Connected ..."; } public void msg(string mesg) { textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg; } private void button1_Click_1(object sender, EventArgs e) { NetworkStream serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + " 这篇关于如何建立TCP客户端服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 00:20