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 ChatServer
{
public partial class Form1 : Form
{
private IPAddress HostIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint ChatServer;
private Socket ChatSocket;
private bool flag = true;
private Socket AcceptedSocket;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
HostIP = IPAddress.Parse("127.0.0.1");
try
{
ChatServer = new IPEndPoint(HostIP, Int32.Parse("8080"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
statusBar.Text = textBox1.Text + " has logoned on.Now begin to listen...";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcess));
thread.Start(); }
catch (Exception ee)
{ statusBar.Text = ee.Message; } }
private void ChatProcess()
{
if (AcceptedSocket.Connected)
{
statusBar.Text = "Ready for the chatting!";
while (flag)
{
Byte[] ReceivedByte = new Byte[64];
AcceptedSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceivedStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
textBox2.AppendText(ReceivedStr + "\r\n"); }
}
}
}
}--------------------------------
每次执行到AcceptedSocket = ChatSocket.Accept();这条语句的时候程序就死掉了
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 ChatServer
{
public partial class Form1 : Form
{
private IPAddress HostIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint ChatServer;
private Socket ChatSocket;
private bool flag = true;
private Socket AcceptedSocket;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
HostIP = IPAddress.Parse("127.0.0.1");
try
{
ChatServer = new IPEndPoint(HostIP, Int32.Parse("8080"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
statusBar.Text = textBox1.Text + " has logoned on.Now begin to listen...";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcess));
thread.Start(); }
catch (Exception ee)
{ statusBar.Text = ee.Message; } }
private void ChatProcess()
{
if (AcceptedSocket.Connected)
{
statusBar.Text = "Ready for the chatting!";
while (flag)
{
Byte[] ReceivedByte = new Byte[64];
AcceptedSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceivedStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
textBox2.AppendText(ReceivedStr + "\r\n"); }
}
}
}
}--------------------------------
每次执行到AcceptedSocket = ChatSocket.Accept();这条语句的时候程序就死掉了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货