我做了一个与socket聊天的应用程序: 服务器创建套接字连接并从任何客户端等待消息 对于服务器:
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.Xml;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace server
{
public partial class Form1 : Form
{
public static byte[] data;
public static byte[] data1;
public static Socket sock;
public delegate void operation(string s);
public delegate void operation2();
public delegate bool verifier();
public Form1()
{
InitializeComponent();
sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress adress = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(adress, 4000);
EndPoint ep = (EndPoint)iep;
sock.Bind(iep);
sock.Listen(10);
sock = sock.Accept();
data1 = new byte[1024];
data = new byte[1024];
Thread.Sleep(2000);
this.Show();
if (sock.Receive(data) > 0)
{
Thread t = new Thread(new ThreadStart(aller));
t.Start();
}
}
private void effectuer(String s)
{
textBox1.Text += "serveur: " + s + "\r\n";
message.Text = "";
}
private void effectuer4(String s)
{
textBox1.Text += "Client: " + s + "\r\n";
message.Text = "";
}
private void aller() {
String s = ASCIIEncoding.ASCII.GetString(data);
if (this.InvokeRequired) Invoke((operation)effectuer4, s);
else effectuer4(s);
//Thread.Sleep(2000);
byte[] data2 = new byte[1024];
if (sock.Receive(data2) > 0 && data2 != data)
{
data = data2;
Thread t = new Thread(new ThreadStart(aller));
t.Start();
}
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
sock.Close();
Application.Exit();
}
private void buttonSend_Click(object sender, EventArgs e)
{
String s = message.Text ;
data1 = System.Text.Encoding.ASCII.GetBytes(s);
sock.Send(data1);
Invoke((operation)effectuer, s);
}
}
}
对于客户端:他向服务器发送空消息并等待服务器的响应
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;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Xml;
namespace client
{
public partial class Form1 : Form
{
public static TcpClient SocketPourClient = null;
public static string ClientMessage;
public static string ServerMessage;
Socket sock;
public static byte[] data;
public static byte[] data1;
public delegate void operation(String s);
public delegate void lancer();
public delegate bool verifier();
public Form1(string ip, int port)
{
InitializeComponent();
IPAddress adress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEnd = new IPEndPoint(adress, 4000);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(ipEnd);
}
catch (SocketException e)
{
MessageBox.Show(e.ToString());
sock.Close();
}
Thread t1 = new Thread(envoi);
t1.Start();
data = new byte[1024];
if (sock.Receive(data) > 0)
{
Thread t=new Thread(new ThreadStart(aller));
t.Start();
}
}
private void aller()
{
String s = ASCIIEncoding.ASCII.GetString(data);
if(this.InvokeRequired) Invoke((operation)effectuer4, s);
else effectuer4(s);
// Thread.Sleep(2000);
byte[] data2 = new byte[1024];
if (sock.Receive(data2) > 0 && data2 != data)
{
data = data2;
Thread t = new Thread(new ThreadStart(aller));
t.Start();
}
}
private void envoi()
{
String s = message.Text ;
data1 = System.Text.Encoding.ASCII.GetBytes(s);
sock.Send(data1);
effectuer(s);
}
private void effectuer(String s)
{
textBox1.Text += "client: " + s + "\r\n";
message.Text = "";
}
private void effectuer4(String s)
{
textBox1.Text += "Server: " + s + "\r\n";
message.Text = "";
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
sock.Close();
Application.Exit();
}
private void buttonSend_Click_1(object sender, EventArgs e)
{
String s = message.Text ;
data1 = System.Text.Encoding.ASCII.GetBytes(s);
sock.Send(data1);
Invoke((operation)effectuer, s);
}
}
}
我的问题是: 我不会认为服务器或客户端没有义务初始化应用程序,但两次可以做到+当我将服务器作为控制台应用程序时它可以工作,但是当我将服务器更改为winforms时,应用程序被阻止! !!!!所以我需要帮助
答案 0 :(得分:0)
不要在Form1 ctor !!中运行listen / accept循环。
在一个单独的监听线程中运行listen / accept。