C# socket 当我第二次点击触发socket时就不行了。这是为什么?第一次触发成功。
客户端:privatevoidpictureBox3_Click(objectsender,EventArgse){try{intserverhost_port=Conv...
客户端:
private void pictureBox3_Click(object sender, EventArgs e)
{
try
{
int serverhost_port = Convert.ToInt32(Settings.setting_textBox3);// 12345;//
string serverhost_ip = Settings.setting_textBox2; //"58.195.50.136"; //
IPAddress client_ip = IPAddress.Parse(serverhost_ip);
IPEndPoint client_ipe = new IPEndPoint(client_ip, serverhost_port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(client_ipe);
string client_sendStr = Settings.setting_textBox4 + BuytextBox.Text;// +"ENDENDENDENDENDENDENDENDENDENDEND";
byte[] client_sendbytes = Encoding.UTF8.GetBytes(client_sendStr);
client.Send(client_sendbytes);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = client.Receive(recvBytes);//接收数据,并将数据存入接收缓冲区。
recvStr += Encoding.UTF8.GetString(recvBytes);
if(recvStr!= null)
{
MessageBox.Show(recvStr);
}
client.Close();
}
服务器:
public void server_wait()
{
try
{
//MessageBox.Show(GetLocalIp());
int server_port = 12345;
string server_ip = GetLocalIp();
IPAddress localhost_ip = IPAddress.Parse(GetLocalIp());//将 IP 地址字符串转换为 IPAddress 实例
IPEndPoint server_ipe = new IPEndPoint(localhost_ip, server_port);//将网络端点表示为 IP 地址和端口号。
Socket server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server_socket.Bind(server_ipe);//绑定
server_socket.Listen(50);//参数指定队列中最多可容纳的等待接受的传入连接
Socket temp = server_socket.Accept();
//MessageBox.Show("Connect success!");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes);// 接收指定字节数的数据,并将数据存入接收缓冲区。
recvStr = Encoding.UTF8.GetString(recvBytes);
textBox1.Text += recvStr;
textBox1.Text += System.Environment.NewLine;
BeepUp.Beep(500, 700);
this.BringToFront();
this.Focus();
string server_sendStr = this.textBox2.Text;
byte[] server_sendbytes = Encoding.UTF8.GetBytes(server_sendStr);
temp.Send(server_sendbytes);
temp.Close();
server_socket.Close();
} 展开
private void pictureBox3_Click(object sender, EventArgs e)
{
try
{
int serverhost_port = Convert.ToInt32(Settings.setting_textBox3);// 12345;//
string serverhost_ip = Settings.setting_textBox2; //"58.195.50.136"; //
IPAddress client_ip = IPAddress.Parse(serverhost_ip);
IPEndPoint client_ipe = new IPEndPoint(client_ip, serverhost_port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(client_ipe);
string client_sendStr = Settings.setting_textBox4 + BuytextBox.Text;// +"ENDENDENDENDENDENDENDENDENDENDEND";
byte[] client_sendbytes = Encoding.UTF8.GetBytes(client_sendStr);
client.Send(client_sendbytes);
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = client.Receive(recvBytes);//接收数据,并将数据存入接收缓冲区。
recvStr += Encoding.UTF8.GetString(recvBytes);
if(recvStr!= null)
{
MessageBox.Show(recvStr);
}
client.Close();
}
服务器:
public void server_wait()
{
try
{
//MessageBox.Show(GetLocalIp());
int server_port = 12345;
string server_ip = GetLocalIp();
IPAddress localhost_ip = IPAddress.Parse(GetLocalIp());//将 IP 地址字符串转换为 IPAddress 实例
IPEndPoint server_ipe = new IPEndPoint(localhost_ip, server_port);//将网络端点表示为 IP 地址和端口号。
Socket server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server_socket.Bind(server_ipe);//绑定
server_socket.Listen(50);//参数指定队列中最多可容纳的等待接受的传入连接
Socket temp = server_socket.Accept();
//MessageBox.Show("Connect success!");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes);// 接收指定字节数的数据,并将数据存入接收缓冲区。
recvStr = Encoding.UTF8.GetString(recvBytes);
textBox1.Text += recvStr;
textBox1.Text += System.Environment.NewLine;
BeepUp.Beep(500, 700);
this.BringToFront();
this.Focus();
string server_sendStr = this.textBox2.Text;
byte[] server_sendbytes = Encoding.UTF8.GetBytes(server_sendStr);
temp.Send(server_sendbytes);
temp.Close();
server_socket.Close();
} 展开
2个回答
展开全部
服务器端等待到了连接以后就直接往下执行了,没有等待客户端连接了。可以在
Socket temp = server_socket.Accept();
以后,创建一个线程,将temp传过去进行具体的通信,在本线程继续server_socket.Accept();
while(true)
{
Socket temp = server_socket.Accept();
Thread th = new Thread(AcceptThread);
th.Start(new object[] { temp });
}
Socket temp = server_socket.Accept();
以后,创建一个线程,将temp传过去进行具体的通信,在本线程继续server_socket.Accept();
while(true)
{
Socket temp = server_socket.Accept();
Thread th = new Thread(AcceptThread);
th.Start(new object[] { temp });
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询