那天我问的聊天例子你还记得吗,我看了你的回帖 改了 可是还是不行啊,你帮我看看好吗,我真的有急用啊,谢谢拉!!!!
[此贴子已经被作者于2006-8-7 10:04:29编辑过]
那天我问的聊天例子你还记得吗,我看了你的回帖 改了 可是还是不行啊,你帮我看看好吗,我真的有急用啊,谢谢拉!!!!
[此贴子已经被作者于2006-8-7 10:04:29编辑过]
呵呵,我是怕你看了烦拉
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.Sockets;
using System.Net;
using System.IO;
using System.Threading;
namespace P2PDemo
{
public partial class Form1 : Form
{
Thread thread;
int backlog;
public Form1()
{
InitializeComponent();
thread = new Thread(new ThreadStart(Listen));
thread.Start();
}
public void Listen()
{
try
{
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 8080));
listener.Listen(backlog);
Socket socket = listener.Accept();
Stream netStream = new NetworkStream(socket);
StreamReader reader = new StreamReader(netStream);
string result = reader.ReadToEnd();
Invoke(new displayinfodelegate(displayinfo), new object[] { result });
}
catch
{
}
}
public void displayinfo(string str)
{
richTextBox1.AppendText(str);
}
delegate void displayinfodelegate(string str);
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Listen();
}
}
}
谢谢解答!
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 8080));
byte[] bytes = new byte[256];
while(true)
{
listener.Listen(1);
Socket socket = listener.Accept();
socket.Receive(bytes);
// Stream netStream = new NetworkStream(socket);
// StreamReader reader = new StreamReader(netStream);
// string result = reader.ReadToEnd();
string result=System.Text.Encoding.Default.GetString(bytes);
Invoke(new displayinfodelegate(displayinfo), new object[] { result });
}