0% found this document useful (0 votes)
128 views6 pages

C TCP允許多人連線SERVER和重複開啟的Client

This document describes code for a C# TCP server that allows multiple client connections. The server code uses TcpListener to accept client connections on port 8888. Each client connection is handled on a separate thread using the handleClinet class. The client code connects to the server and sends messages when a button is clicked. The server receives the message, responds with a message containing a request count, and sends it back to the client.

Uploaded by

KuanTing Kuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views6 pages

C TCP允許多人連線SERVER和重複開啟的Client

This document describes code for a C# TCP server that allows multiple client connections. The server code uses TcpListener to accept client connections on port 8888. Each client connection is handled on a separate thread using the handleClinet class. The client code connects to the server and sends messages when a button is clicked. The server receives the message, responds with a message containing a request count, and sends it back to the client.

Uploaded by

KuanTing Kuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[C#] TCP允許多人連線SERVER和重複開啟的Client

charleslin74.pixnet.net/blog/post/460014727-[c%23]-tcp允許多人連線server和重複開啟的client

痞客興的部落格

程式碼擷錄自jashliao痞客邦部落格

SERVER

using System;

using System.Threading;

using System.Net.Sockets;

using System.Text;

namespace csharp_multi_threaded_server_socket

//資料來源 http://csharp.net-informations.com/communications/csharp-multi-
threaded-server-socket.htm

class Program

static void Main(string[] args)

TcpListener serverSocket = new TcpListener(8888);

TcpClient clientSocket = default(TcpClient);

1/6
int counter = 0;

serverSocket.Start();

Console.WriteLine(" >> " + "Server Started");

counter = 0;

while (true)

counter += 1;

clientSocket = serverSocket.AcceptTcpClient();

Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + "


started!");

handleClinet client = new handleClinet();

client.startClient(clientSocket, Convert.ToString(counter));

clientSocket.Close();

serverSocket.Stop();

Console.WriteLine(" >> " + "exit");

Console.ReadLine();

//Class to handle each client request separatly

public class handleClinet

TcpClient clientSocket;

string clNo;

public void startClient(TcpClient inClientSocket, string clineNo)

this.clientSocket = inClientSocket;

2/6
this.clNo = clineNo;

Thread ctThread = new Thread(doChat);

ctThread.Start();

private void doChat()

int requestCount = 0;

byte[] bytesFrom = new byte[10025];

string dataFromClient = null;

Byte[] sendBytes = null;

string serverResponse = null;

string rCount = null;

requestCount = 0;

while ((true))

try

requestCount = requestCount + 1;

NetworkStream networkStream = clientSocket.GetStream();

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

dataFromClient = dataFromClient.Substring(0,
dataFromClient.IndexOf("$"));

Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

rCount = Convert.ToString(requestCount);

serverResponse = "Server to clinet(" + clNo + ") " + rCount;

sendBytes = Encoding.ASCII.GetBytes(serverResponse);

3/6
networkStream.Write(sendBytes, 0, sendBytes.Length);

networkStream.Flush();

Console.WriteLine(" >> " + serverResponse);

catch (Exception ex)

clientSocket.Close();//jashliao修改

Console.WriteLine(" >> " + "Server to clinet(" + clNo + ") closed..." );//


jashliao修改

break; // jashliao修改

CLIENT

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net.Sockets;

namespace csharp_multi_threaded_client_socket

4/6
//資料來源 http://csharp.net-informations.com/communications/csharp-multi-
threaded-client-socket.htm

public partial class Form1 : Form

System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

NetworkStream serverStream;

public Form1()

InitializeComponent();

private void Form1_Load(object sender, EventArgs e)

msg("Client Started");

clientSocket.Connect("127.0.0.1", 8888);

label1.Text = "Client Socket Program - Server Connected ...";

private void button1_Click(object sender, EventArgs e)

NetworkStream serverStream = clientSocket.GetStream();

byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from


Client$");

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("Data from Server : " + returndata);

5/6
}

public void msg(string mesg)

textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

clientSocket.GetStream().Close();// jashliao修改

clientSocket.Close();// jashliao修改

6/6

You might also like