[英]Communication between server and client connection C#
我正在為我的學校項目做某事,它需要我構建一個C#應用程序,該應用程序允許服務器(偵聽器)和客戶端之間的通信。 問題是,我已經這樣做了,但它根本不起作用,它拋出了太多錯誤要糾正,並且肯定編碼不正確,我以前從未做過如此草率的事情。
我還希望許多客戶端能夠同時訪問服務器,因此有一個活躍人員的客戶端列表。 我在網上看到了一個例子,但是對於我要完成的任務來說太復雜了。 我只希望與客戶端和服務器之間進行簡單的文本通信,並在需要時發送信息。 我不希望它停留在while循環中等待信息發送等等。
對不起,如果我不好解釋。 謝謝。
編輯
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace CentralHubGUI
{
class chCon
{
static TcpClient clientCon;
static NetworkStream stream;
public static bool currentlyConnected;
static string buffer;
string ip = "";
int port = 0;
Thread thread1 = new Thread(receiveData_Stage1);
//Thread thread2 = new Thread();
//Thread thread3 = new Thread();
public chCon(string ipAddress, int portNumber)
{
ip = ipAddress;
port = portNumber;
connectToConsole();
}
public void connectToConsole()//mitigates the connection
{
if (ip.Trim() == "")
ip = "127.0.0.1";
if (port == 0)
port = 2647;
try
{
clientCon = new TcpClient(ip, port);
stream = clientCon.GetStream();
sendData("#101" + (char)13); //first bit of data is sent on accepted client
Thread retrieveData = new Thread(receiveData_Stage1);
retrieveData.Start();
currentlyConnected = true;
thread1.Start(); //starting to read the server for information
}
catch(Exception e) // if the connection being naughty ;)
{
currentlyConnected = false;
MessageBox.Show("Exception caught:\n" + e);
}
}
public void disconnectFromConsole()
{
try
{
if (clientCon.Connected || clientCon.Connected == null)
{
thread1.Abort();
byte[] msg = System.Text.Encoding.ASCII.GetBytes("#103");
stream.Write(msg, 0, msg.Length);
stream.Close();
clientCon.Close();
currentlyConnected = false;
}
else
{
MessageBox.Show("Cannot disconnect from a connection that has not started.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error thrown during disconnection - UNKNOWN");
}
}
internal void sendData(string data)//sends data to the server
{
Byte[] dataConv = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(dataConv, 0, dataConv.Length);
}
public static void receiveData_Stage1() //builds a buffer through a loop
{
while (true)
{
Byte[] response = new Byte[256];
string responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(response, 0, response.Length);
responseData = System.Text.Encoding.ASCII.GetString(response);
if(responseData.Trim() != "")
buffer = buffer + responseData;
}
}
public static string receiveData_Stage2() //requests the buffer through a return value string
{
string bufferTemp;
bufferTemp = buffer;
buffer = string.Empty;
return bufferTemp;
}
public void closeConnection()
{
stream.Close();
clientCon.Close();
thread1.Abort();
}
}
/*
while (true)
{
if (Program.currentlyConnected == true)
{
Thread.Sleep(100);
buffer = Program.receiveData_Stage2();
if (buffer != "")
richTxtBoxConsole.AppendText(buffer + "\n");
}
else
{
guiConsoleWriteLine("Connection is not active.");
break;
}
}
*/
}
根據您的解釋,我無法真正弄清您想要什么,但是根據我的猜測,可以說這取決於您想要使用的技術。 我建議使用WCF Windows Communication Foundation。 您可以在.NET Foundation 入門教程中找到一些不錯的文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.