C#网络-我的表单(客户端程序)不';启动服务器程序时不显示
我正在开发一个网络应用程序。 服务器程序给出问题,然后客户端输入答案。 实际上,服务器程序和客户端程序都是控制台程序。 但是,当我将客户端程序更改为Form程序时,它不会显示。(仅当我打开服务器程序时!) 它在我不启动服务器程序时工作 这是我的服务器程序代码C#网络-我的表单(客户端程序)不';启动服务器程序时不显示,c#,multithreading,forms,sockets,console,C#,Multithreading,Forms,Sockets,Console,我正在开发一个网络应用程序。 服务器程序给出问题,然后客户端输入答案。 实际上,服务器程序和客户端程序都是控制台程序。 但是,当我将客户端程序更改为Form程序时,它不会显示。(仅当我打开服务器程序时!) 它在我不启动服务器程序时工作 这是我的服务器程序代码 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace Server {
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Server
{
public class TaskInfo
{
public Socket client;
public int thread_num;
public int q_num;
public ManualResetEvent doneEvent;
public TaskInfo(Socket c, int n, int q, ManualResetEvent d)
{
client = c;
thread_num = n;
q_num = q;
doneEvent = d;
}
}
public class ServerTCP
{
static int num_client = 2;
static bool[] check_co_client = new bool[num_client];
static int num_co_client = 0;
static void ClientHandler(Object task_info)
{
TaskInfo ti = (TaskInfo)task_info;
// Casting of object
Socket client = (Socket)ti.client;
IPEndPoint endpoint = (IPEndPoint)client.RemoteEndPoint;
//Console.WriteLine("{0} connected at port {1}", endpoint.Address, endpoint.Port);
int index = ti.q_num;
byte[] buffer = new byte[256];
int byteRecv = 0;
string message = "";
string[] questions = new string[5];
questions[0] = "0. What is 5+6?";
questions[1] = "1. What is 1+3?";
questions[2] = "2. What is 5+3?";
questions[3] = "3. What is 1+1?";
questions[4] = "4. What is 9-5?";
string[] answers = new string[5];
answers[0] = "11";
answers[1] = "4";
answers[2] = "8";
answers[3] = "2";
answers[4] = "4";
try
{
// Send message to client
buffer = Encoding.ASCII.GetBytes(questions[index]);
client.Send(buffer, buffer.Length, SocketFlags.None);
// Receive data from client
byteRecv = client.Receive(buffer);
message = Encoding.ASCII.GetString(buffer, 0, byteRecv);
if (message == answers[ti.q_num])
{
check_co_client[ti.thread_num] = true;
}
else
{
check_co_client[ti.thread_num] = false;
}
ti.doneEvent.Set();
}
catch (SocketException se)
{
Console.WriteLine("{0}: {1} [{2}:{3}]", se.ErrorCode, se.Message, endpoint.Address, endpoint.Port);
}
finally
{
// Close socket
//client.Close();
}
}
public static void Main()
{
TcpListener listener = null;
try
{
// Create new instance and start listening
listener = new TcpListener(IPAddress.Any, 8080);
listener.Start();
Console.WriteLine("<Waiting for clients connection>");
// Accept the client connection
Socket[] client = new Socket[num_client];
int count = 0;
while (count < num_client)
{
client[count] = listener.AcceptSocket();
count++;
}
ManualResetEvent[] doneEvents = new ManualResetEvent[num_client];
// Start threads
Thread[] t = new Thread[num_client];
for (int q_num = 0; q_num < 5; q_num++)
{
for (int i = 0; i < num_client; i++)
{
doneEvents[i] = new ManualResetEvent(false);
TaskInfo ti = new TaskInfo(client[i], i, q_num, doneEvents[i]);
t[i] = new Thread(ClientHandler);
t[i].Start(ti);
}
WaitHandle.WaitAll(doneEvents);
Thread.Sleep(2000);
// count clients who enter correct answer
for (int index = 0; index < num_client; index++)
{
if (check_co_client[index])
{
num_co_client++;
}
}
Console.WriteLine("number of correct answers: {0}", num_co_client);
num_co_client = 0;
}
}
catch (SocketException se)
{
Console.WriteLine("Error Code: {0}", se.ErrorCode);
Console.WriteLine("Message: {0}", se.Message);
}
finally
{
listener.Stop();
}
Console.ReadLine(); // Pause for IDE
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client_Form
{
public partial class Client_Form : Form
{
TcpClient client = null;
NetworkStream stream = null;
int count = 0;
public Client_Form()
{
InitializeComponent();
Start_Program();
}
private void Start_Program()
{
try
{
// Connect to server
client = new TcpClient("127.0.0.1", 8080);
//Console.WriteLine("<Connected to server>");
textBox.Text = "<Connected to server>";
stream = client.GetStream();
byte[] buffer = new byte[256];
int byteRecv = 0;
string message = "";
int q_count = 5;
while (count < q_count)
{
while ((byteRecv = stream.Read(buffer, 0, buffer.Length)) == 0)
{
textBox.Text = "Waiting for other client";
Thread.Sleep(3000);
}
// Get data from server
//byteRecv = stream.Read(buffer, 0, buffer.Length);
message = Encoding.ASCII.GetString(buffer, 0, byteRecv);
Co_Game_Question.Text = message;
}
// Close stream and connection
stream.Close();
client.Close();
//Console.WriteLine("<Connection closed>");
textBox.Text = "<Connection closed>";
}
catch (SocketException se)
{
//Console.WriteLine("Error: {0}", se.SocketErrorCode);
//Console.WriteLine("Error Code: {0}", se.ErrorCode);
//Console.WriteLine("Message: {0}", se.Message);
textBox.Text = "Error Message: " + se.Message;
}
}
private void Btn_Enter_Click(object sender, EventArgs e)
{
// Send message to server
//Console.Write("Answer {0}> ", count);
//textBox.Text = "Answer " + count + "> ";
byte[] data = Encoding.ASCII.GetBytes(User_answer_box.Text);
stream.Write(data, 0, data.Length);
count++;
}
}
}
使用系统;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用系统线程;
命名空间服务器
{
公共类任务信息
{
公共套接字客户端;
公共int线程数;
公共int q_num;
公共手册重置事件DoneeEvent;
公共任务信息(插座c、int n、int q、手动重置事件d)
{
客户=c;
线程数=n;
q_num=q;
doneEvent=d;
}
}
公共类服务器TCP
{
静态int num_client=2;
静态bool[]check_co_client=新bool[num_client];
静态int num_co_client=0;
静态void ClientHandler(对象任务信息)
{
任务信息ti=(任务信息)任务信息;
//物体的铸造
socketclient=(Socket)ti.client;
IPEndPoint端点=(IPEndPoint)client.RemoteEndPoint;
//WriteLine(“{0}连接在端口{1}”,endpoint.Address,endpoint.port);
int index=ti.q_num;
字节[]缓冲区=新字节[256];
int byteRecv=0;
字符串消息=”;
字符串[]问题=新字符串[5];
问题[0]=“0.5+6是什么?”;
问题[1]=“1.什么是1+3?”;
问题[2]=“2.什么是5+3?”;
问题[3]=“3.什么是1+1?”;
问题[4]=“4.9-5是什么?”;
字符串[]答案=新字符串[5];
答案[0]=“11”;
答案[1]=“4”;
答案[2]=“8”;
答案[3]=“2”;
答案[4]=“4”;
尝试
{
//向客户端发送消息
buffer=Encoding.ASCII.GetBytes(问题[index]);
发送(buffer,buffer.Length,SocketFlags.None);
//从客户端接收数据
byteRecv=client.Receive(缓冲区);
message=Encoding.ASCII.GetString(缓冲区,0,byteRecv);
如果(消息==答案[ti.q_num])
{
检查_co_client[ti.thread_num]=true;
}
其他的
{
检查_co_client[ti.thread_num]=false;
}
ti.doneEvent.Set();
}
捕获(SocketException se)
{
Console.WriteLine(“{0}:{1}[{2}:{3}]”,se.ErrorCode,se.Message,endpoint.Address,endpoint.Port);
}
最后
{
//闭合插座
//client.Close();
}
}
公共静态void Main()
{
TcpListener侦听器=null;
尝试
{
//创建新实例并开始侦听
侦听器=新的TcpListener(IPAddress.Any,8080);
listener.Start();
控制台。写线(“”);
//接受客户端连接
套接字[]客户端=新套接字[num_client];
整数计数=0;
同时(计数
这是我的客户端程序代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Server
{
public class TaskInfo
{
public Socket client;
public int thread_num;
public int q_num;
public ManualResetEvent doneEvent;
public TaskInfo(Socket c, int n, int q, ManualResetEvent d)
{
client = c;
thread_num = n;
q_num = q;
doneEvent = d;
}
}
public class ServerTCP
{
static int num_client = 2;
static bool[] check_co_client = new bool[num_client];
static int num_co_client = 0;
static void ClientHandler(Object task_info)
{
TaskInfo ti = (TaskInfo)task_info;
// Casting of object
Socket client = (Socket)ti.client;
IPEndPoint endpoint = (IPEndPoint)client.RemoteEndPoint;
//Console.WriteLine("{0} connected at port {1}", endpoint.Address, endpoint.Port);
int index = ti.q_num;
byte[] buffer = new byte[256];
int byteRecv = 0;
string message = "";
string[] questions = new string[5];
questions[0] = "0. What is 5+6?";
questions[1] = "1. What is 1+3?";
questions[2] = "2. What is 5+3?";
questions[3] = "3. What is 1+1?";
questions[4] = "4. What is 9-5?";
string[] answers = new string[5];
answers[0] = "11";
answers[1] = "4";
answers[2] = "8";
answers[3] = "2";
answers[4] = "4";
try
{
// Send message to client
buffer = Encoding.ASCII.GetBytes(questions[index]);
client.Send(buffer, buffer.Length, SocketFlags.None);
// Receive data from client
byteRecv = client.Receive(buffer);
message = Encoding.ASCII.GetString(buffer, 0, byteRecv);
if (message == answers[ti.q_num])
{
check_co_client[ti.thread_num] = true;
}
else
{
check_co_client[ti.thread_num] = false;
}
ti.doneEvent.Set();
}
catch (SocketException se)
{
Console.WriteLine("{0}: {1} [{2}:{3}]", se.ErrorCode, se.Message, endpoint.Address, endpoint.Port);
}
finally
{
// Close socket
//client.Close();
}
}
public static void Main()
{
TcpListener listener = null;
try
{
// Create new instance and start listening
listener = new TcpListener(IPAddress.Any, 8080);
listener.Start();
Console.WriteLine("<Waiting for clients connection>");
// Accept the client connection
Socket[] client = new Socket[num_client];
int count = 0;
while (count < num_client)
{
client[count] = listener.AcceptSocket();
count++;
}
ManualResetEvent[] doneEvents = new ManualResetEvent[num_client];
// Start threads
Thread[] t = new Thread[num_client];
for (int q_num = 0; q_num < 5; q_num++)
{
for (int i = 0; i < num_client; i++)
{
doneEvents[i] = new ManualResetEvent(false);
TaskInfo ti = new TaskInfo(client[i], i, q_num, doneEvents[i]);
t[i] = new Thread(ClientHandler);
t[i].Start(ti);
}
WaitHandle.WaitAll(doneEvents);
Thread.Sleep(2000);
// count clients who enter correct answer
for (int index = 0; index < num_client; index++)
{
if (check_co_client[index])
{
num_co_client++;
}
}
Console.WriteLine("number of correct answers: {0}", num_co_client);
num_co_client = 0;
}
}
catch (SocketException se)
{
Console.WriteLine("Error Code: {0}", se.ErrorCode);
Console.WriteLine("Message: {0}", se.Message);
}
finally
{
listener.Stop();
}
Console.ReadLine(); // Pause for IDE
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client_Form
{
public partial class Client_Form : Form
{
TcpClient client = null;
NetworkStream stream = null;
int count = 0;
public Client_Form()
{
InitializeComponent();
Start_Program();
}
private void Start_Program()
{
try
{
// Connect to server
client = new TcpClient("127.0.0.1", 8080);
//Console.WriteLine("<Connected to server>");
textBox.Text = "<Connected to server>";
stream = client.GetStream();
byte[] buffer = new byte[256];
int byteRecv = 0;
string message = "";
int q_count = 5;
while (count < q_count)
{
while ((byteRecv = stream.Read(buffer, 0, buffer.Length)) == 0)
{
textBox.Text = "Waiting for other client";
Thread.Sleep(3000);
}
// Get data from server
//byteRecv = stream.Read(buffer, 0, buffer.Length);
message = Encoding.ASCII.GetString(buffer, 0, byteRecv);
Co_Game_Question.Text = message;
}
// Close stream and connection
stream.Close();
client.Close();
//Console.WriteLine("<Connection closed>");
textBox.Text = "<Connection closed>";
}
catch (SocketException se)
{
//Console.WriteLine("Error: {0}", se.SocketErrorCode);
//Console.WriteLine("Error Code: {0}", se.ErrorCode);
//Console.WriteLine("Message: {0}", se.Message);
textBox.Text = "Error Message: " + se.Message;
}
}
private void Btn_Enter_Click(object sender, EventArgs e)
{
// Send message to server
//Console.Write("Answer {0}> ", count);
//textBox.Text = "Answer " + count + "> ";
byte[] data = Encoding.ASCII.GetBytes(User_answer_box.Text);
stream.Write(data, 0, data.Length);
count++;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
Net系统;
使用System.Net.Sockets;
使用系统线程;
名称空间客户端表单
{
公共部分类客户端表单:表单
{
TcpClient client=null;
NetworkStream=null;
整数计数=0;
公共客户表格()
{
初始化组件();
启动_程序();
}
私有无效启动程序()
{
尝试
{
//连接到服务器
客户机=新的TCP客户机(“127.0.0.1”,8080);