服务器端程序: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 ServerTest
{
    public partial class ServerT : Form
    {        private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
        private int IPPort = 10001;
        private IPEndPoint myServer;
        private Socket mySocket;
        private Socket Test;
        private String Msg;        public ServerT()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            formDo();
        }        private void formDo() {
            try
            {
                Thread vThread = new Thread(new ThreadStart(acc));
                vThread.Start();
            }
            catch (Exception ee)
            {
                rTB1.AppendText("Error: " + ee.Message + "\r\n");
            }
        }
        
        private void invokeRTB1()
        {
            rTB1.AppendText(Msg);
        }        private void acc()
        {
            myServer = new IPEndPoint(vAddr, IPPort);
            mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySocket.Bind(myServer);
            mySocket.Listen(5);
            Msg = "Wait Connection ...\r\n";
            rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
            int vConnected = 0;
            while (true)
            {
                Test = mySocket.Accept();
                if (Test.Connected)
                {
                    if (vConnected == 0)
                    {
                        vConnected = 1;
                        sendM("Connection Successfully\r\n");
                    }
                    Msg = "Connection Successfully\r\n";
                    rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                    Thread vThread = new Thread(new ThreadStart(connectionNow));
                    vThread.Start();
                }
            }
        }        private void connectionNow()
        {
            while (true)
            {
                Byte[] testReadStr = new byte[64];
                NetworkStream readStr = new NetworkStream(Test);
                readStr.Write(testReadStr, 0, testReadStr.Length);
                Msg = Encoding.Unicode.GetString(testReadStr);
                if (Msg.Length != 0)
                {
                    Msg = Msg + "\r\n";
                    rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                }
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            sendM(tB1.Text+"\r\n");
            rTB1.AppendText(tB1.Text + "\r\n");
        }        private void sendM(String Message)
        {
            NetworkStream sendStr = new NetworkStream(Test);
            Byte[] testSendStr = new byte[64];
            String sendMessage = Message;
            //testSendStr = UnicodeEncoding.Unicode.GetBytes(sendMessage);
            testSendStr = Encoding.Unicode.GetBytes(sendMessage);
            sendStr.Write(testSendStr, 0, testSendStr.Length);
        }
    }
    public delegate void RichTextBoxStreamType(); 
}客户端程序: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 ClientTest
{
    public partial class Form1 : Form
    {
        private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
        private int IPPort = 10001;
        private IPEndPoint myServer;
        private Socket mySocket;
        private String Msg;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            formDo();
        }        private void Butt1_Click(object sender, EventArgs e)
        {        }
        
        private void invokeRTB1()
        {
            rTB1.AppendText(Msg);
        }        private void formDo()
        {
            try
            {
                Thread vThread = new Thread(new ThreadStart(acc));
                vThread.Start();
            }
            catch (Exception ee)
            {
                rTB1.AppendText("Error: " + ee.Message + "\r\n");
            }
        }        private void acc()
        {
            try
            {
                myServer = new IPEndPoint(vAddr, IPPort);
                mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mySocket.Connect(myServer);
                Thread vThread = new Thread(new ThreadStart(connectionNow));
                vThread.Start();
                Msg = "连接成功\r\n";
                rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
            }
            catch (Exception ee)
            {
                Msg = ee.Message + "\r\n";
                rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
            }
        }        private void connectionNow()
        {
            while (true)
            {
                try
                {
                    Byte[] testReadStr = new byte[64];
                    NetworkStream readStr = new NetworkStream(mySocket);
                    readStr.Write(testReadStr, 0, testReadStr.Length);
                    Msg = Encoding.Unicode.GetString(testReadStr);
                    if (Msg.Length != 0)
                    {
                        Msg = Msg + "\r\n";
                        rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                    }
                }
                catch (Exception ee)
                {
                    mySocket.Close();
                    Msg = ee.Message + "\r\n";
                    rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                    break;
                }
            }
        }        public delegate void RichTextBoxStreamType(); 
    }
}服务器端有三个控件,一个rTB1是RichTextBox,用来记录显示发送和接收的信息,一个tB1是用来发送信息的,还有一个就是按钮。
客户端也有三个控件,功能和服务器端的一样。客户端发送信息的部分还没写,仅仅只是连接上服务器,然后接收服务器端的信息着关就过不了。连接上后,服务器端在tB1中输入一串字符,按按钮发送后,程序就死了。字符串也没有传送到客户端。我如果不用自己写的客户端连,而是用Command窗口中telnet命令连接上后,服务器端却可以正常发送字符串。
请问这是怎么回事啊?是不是客户端接收字符串的代码出错了?

解决方案 »

  1.   

    额,建议Encoding.Unicode改成Encoding.Default,这样,中英文都能正常显示出来。个人感觉这个循环有问题,
                while (true)
                {
                    Test = mySocket.Accept();
                    if (Test.Connected) 
                    {
                        if (vConnected == 0)
                        {
                            vConnected = 1;
                            sendM("Connection Successfully\r\n");
                        }
                        Msg = "Connection Successfully\r\n";
                        rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                        Thread vThread = new Thread(new ThreadStart(connectionNow));  //一旦接收连接成功,即Test.Connected =1 以后,这里永远都在做...
                        vThread.Start();
                    }
                }
      

  2.   

    http://blog.programfan.com/article.asp?id=23153
      

  3.   

    他不是显不显示出来的问题,信息可能就没收到,或没发出,因为服务器端一发信息,程序就死掉了。可如果是通过Command窗口telnet上去时,服务器端发的信息就可以显示在Command窗口中。
      

  4.   

    没人能帮我看看么?是不是acc函数里面监听出了错?我第一次写socket程序,大家帮帮忙,谢谢!
      

  5.   

    c#写的是ftp的,可以做个参考。 
    http://www.mysticboy.cn/article.asp?id=7
      

  6.   

    vb2005写的和vb6通讯的。也可以做以参考。 不知道你用过vb没。 不过应该可以看懂呢。 
    http://www.mysticboy.cn/article.asp?id=9
      

  7.   

    我试过用VB作客户端的,是可以接收到C#服务器端的信息,但传过来的信息长度不对。我第一次写socket程序。就遇到这么麻烦的事情。奇怪了,telnet连接上去接收信息就没问题,C#写的客户端就不行。
      

  8.   

    //client 
    private void connectionNow()
            {
                while (true)
                {
                    try
                    {
                        Byte[] testReadStr = new byte[64];
                        NetworkStream readStr = new NetworkStream(mySocket);
                        readStr.Write(testReadStr, 0, testReadStr.Length);//?????Read or Write
                        Msg = Encoding.Unicode.GetString(testReadStr);
                        if (Msg.Length != 0)
                        {
                            Msg = Msg + "\r\n";
                            rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                        }
                    }
                    catch (Exception ee)
                    {
                        mySocket.Close();
                        Msg = ee.Message + "\r\n";
                        rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
                        break;
                    }
                }
            }
      

  9.   

    同步socket 在每次从流中读数据之前最好先判断流中是否可读。否则会阻塞程序。程序会假死。
      

  10.   

    谢谢Qim(莫名-想星星) 
    一语惊醒梦中人,都是Copy惹的祸