本帖最后由 蓝汝逸 于 2012-3-30 18:47 编辑
聊天程序服务器端分享下代码,看有没有错误!
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;
using System.Net.Sockets;
namespace socket
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnbeginlisten_Click(object sender, EventArgs e)
{
Socket socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(txtip.Text.Trim());
IPEndPoint endpoint=new IPEndPoint(address,int.Parse(txtport.Text.Trim()));
socketwatch.Bind(endpoint);
socketwatch.Listen(10);
Socket sokconnection=socketwatch.Accept();
showmsg("客户端连接成功!");
}
void showmsg(string msg)
{
txtmsg.AppendText(msg + "\r\n");
}
}
}
|