求教高手:
我做了一个Window Form聊天的程序(单机版),我想把它升级成能在局域网中能聊天的东西,怎么做?我查过,这似乎要用到TCP通信,有没有实例和详细的讲解啊,介绍一下,讲一讲啊....

解决方案 »

  1.   

    使用TcpClient 和TcpListener 这两个类就可以实现了
      

  2.   

    服务器:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace ConsoleApplication1
    {
        class Program1
        {
            static void Main(string[] args)
            {
                int receive;
                byte[] date = new byte[1024];
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
                Socket newsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                newsocket.Bind(iep);
                newsocket.Listen(10);
                Console.WriteLine("Waiting for a message for client");
                Socket client = newsocket.Accept();
                IPEndPoint clintiep = (IPEndPoint)client.RemoteEndPoint;
                Console.WriteLine("Connected with {0} at port {1}", clintiep.Address, clintiep.Port);
                string welcome = " welecome to my sever";
                date = Encoding.ASCII.GetBytes(welcome);
                client.Send(date, date.Length, SocketFlags.None);
                while (true)
                {
                    date = new byte[1024];
                    receive = client.Receive(date);
                    if (receive == 0)
                    {
                        break;
                    }
                    Console.WriteLine(Encoding.ASCII.GetString(date, 0, receive));
                    string input = "";
                    input = Console.ReadLine();
                    date = Encoding.ASCII.GetBytes(input);
                    receive = date.Length;
                    client.Send(date, receive, SocketFlags.None);
                }
                Console.WriteLine("disconnceted with {0}", clintiep.Address);
                client.Close();
                newsocket.Close();
            }
        }
    }
    客户端:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;namespace client
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte[] data = new byte[1024];
                string input, stringdate;
                IPAddress local = IPAddress.Parse("192.168.1.227");
                IPEndPoint iep = new IPEndPoint(local,9050);
                Socket sever=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    sever.Connect(iep);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("无法连接到服务器");
                    Console.WriteLine(e.ToString());
                }
                finally
                {
                
                }
                int recive = sever.Receive(data);
                stringdate = Encoding.ASCII.GetString(data, 0, recive);
                while (true)
                {
                    input = Console.ReadLine();
                    if (input == "exit")
                    {
                        break;
                    }
                    sever.Send(Encoding.ASCII.GetBytes(input));
                    int rec = sever.Receive(data);
                    Console.WriteLine(Encoding.ASCII.GetString(data, 0, rec));
                }
                Console.WriteLine("断开与服务器的连接.....");
                sever.Close();
            }
        }
    }
    这是控制台程序,好久以前的不知道行不行
      

  3.   

    找到一个文章,我觉得蛮好,分享一下:http://topic.csdn.net/u/20110520/14/caa51c8c-c4f9-4d39-9a19-2cae29987588.html
    我的个人理解:
    将原来DAL层直接插到数据库中的代码改为,先将要发送的信息转换为byte[]二进制数组,用Socket发到服务器端,由服务器段将收到的byte[]二进制数组在转换为string插入数据库.
    是否可以这样理解?
    一个较为弱智的问题:
    服务器段代码是在控制台中写吗?
      

  4.   

    继续求教啊
    代码如下:客户端private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    //  获取信息
                    string messages = this.txtMessages.Text;
                    //  端口
                    int port = 8080;
                    //  IP号
                    string host = "127.0.0.1";
                    //  IP地址
                    IPAddress ip = IPAddress.Parse(host);
                    //  把ip和端口转化为IPEndPoint实例
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    //  创建一个Socket
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //  提示
                    this.txtMessages.Text += "\r\n连接中......\r\n";
                    //  连接到服务器
                    c.Connect(ipe);                //  信息转化为二进制数据
                    byte[] messagesByte = Encoding.ASCII.GetBytes(messages);
                    //  提示
                    this.txtMessages.Text += "发送中.......\r\n";
                    //  发送
                    c.Send(messagesByte, messagesByte.Length, 0);                //  服务器的返回信息
                    string receiveServerInfo = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    //  接收服务器返回的信息
                    bytes = c.Receive(recvBytes, recvBytes.Length, 0);
                    //  结果信息,将byte转化为string
                    receiveServerInfo += Encoding.ASCII.GetString(recvBytes, recvBytes.Length, 0);
                    //  显示接收的返回信息
                    this.txtMessages.Text += receiveServerInfo;
                    c.Close();
                }
                catch (ArgumentNullException ae)
                {
                    MessageBox.Show("错误:" + ae.Message);
                }
                catch (SocketException se)
                {
                    MessageBox.Show("Socket错误:" + se.Message);
                }
            }服务器端
            static void Main(string[] args)
            {
                try
                {
                    //  端口号
                    int port = 8080;
                    //  ip号
                    string host = "127.0.0.1";
                    //  ip地址
                    IPAddress ip = IPAddress.Parse(host);
                    //  将端口号和IP转换为IPEndPoint实例
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    //  创建一个Socket对象
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //  绑定端口
                    s.Bind(ipe);
                    //  开始监听
                    s.Listen(0);
                    //  提示
                    Console.WriteLine("登台连接...");
                    //  为新建连接创建新的Socket
                    Socket temp = s.Accept();
                    //  提示
                    Console.WriteLine("得到连接...");
                    //  接收信息
                    string srecvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    //  从客户端接收信息
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                    //  转换为字符串
                    srecvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    string sendStr = "OK!Client SendMessage Sucessful!";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    //  返回客户端信息
                    temp.Send(bs, bs.Length, 0);
                    temp.Close();
                    s.Close();
                }
                catch (ArgumentNullException ae)
                {
                    Console.WriteLine("错误:" + ae.Message);
                }
                catch (SocketException se)
                {
                    Console.WriteLine("Socket错误:" + se.Message);
                }
            }
    最后在连接发送后,会出现“远程主机强迫关闭了一个现有的链接”的错误,前各位高手指点一下.....