跳转到内容
-
-
-
-
-
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
-
- namespace WinNet
- {
- public partial class ServerFrm : Form
- {
- private Thread processor;
- private TcpListener tcpListener;
- string clientStr;
- public ServerFrm()
- {
- InitializeComponent();
- }
-
- private void ServerFrm_Load(object sender, EventArgs e)
- {
-
- processor = new Thread(new ThreadStart(StartListening));
- processor.Start();
- }
- private void StartListening()
- {
-
- tcpListener = new TcpListener(IPAddress.Any, 8888);
- tcpListener.Start();
-
- while (true)
- {
- try
- {
-
- TcpClient tcpClient = tcpListener.AcceptTcpClient();
-
- NetworkStream clientStream = tcpClient.GetStream();
-
- byte[] buffer = new byte[10];
- clientStream.Read(buffer, 0, 10);
-
- clientStr= System.Text.Encoding.ASCII.GetString(buffer);
-
- buffer = System.Text.Encoding.ASCII.GetBytes(“helloclint”.ToCharArray());
- clientStream.Write(buffer, 0, 10);
-
- this.Text = clientStr;
-
- tcpClient.Close();
- clientStream.Close();
- }
- catch
- {
- }
- }
- }
- }
- }