当前位置:编程学习 > C#/ASP.NET >>

崩溃,我实在是不知道,这是哪里出错了!!


这是我做的一个TCP协议的简单的网络客户端,和服务器端程序。
但是在本地可以运行,但在网络上的两台机子,却无法连接。???
我都找了好久原因了,究竟是哪里啊啊啊。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;

namespace Client
{
    class CNTrans
    {
        /// <summary>
        /// 服务器IP
        /// </summary>
        private static  string cServerIP;
        /// <summary>
        /// 发送或接收流的大小
        /// </summary>
        private static  int cDataSize;
        /// <summary>
        /// 端口
        /// </summary>
        private static  int cPort;
        /// <param name="serverIP">服务器的IP</param>
        /// <param name="datasize">发送或接收流的大小</param>
        /// <param name="port">端口号</param>
        public void cSet_ADDR(string serverIP, int datasize, int port)
        {
            cServerIP = serverIP;
            cDataSize = datasize;
            cPort = port;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">要发送的消息</param>
        public void cSend_MSG(string message)
        {
            TcpClient client = new TcpClient();
            client.Connect(cServerIP,cPort);

            NetworkStream clientStream = client.GetStream();
            byte[] requestBuffer = Encoding.GetEncoding("UTF-8").GetBytes(message);
            clientStream.Write(requestBuffer, 0, requestBuffer.Length);

            clientStream.Close();
            client.Close();
        }

        /// <summary>
        /// 接收消息,返回消息值
        /// </summary>
        public string cRecv_MSG()
        {
            TcpClient client = new TcpClient();
            client.Connect(cServerIP,cPort);
            NetworkStream clientStream = client.GetStream();

            int bufferSize = cDataSize;
            byte[] responseBuffer = new byte[bufferSize];
            MemoryStream memStream = new MemoryStream();
            int bytesRead = 0;
            do
            {
                bytesRead = clientStream.Read(responseBuffer, 0, bufferSize);
                memStream.Write(responseBuffer, 0, bytesRead);
            } while (bytesRead > 0);

            clientStream.Close();
            client.Close();

            return Encoding.GetEncoding("UTF-8").GetString(responseBuffer);
        }

        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="filePath_assend">发送的文件的路径</param>
        public void cSend_File(string filePath_assend)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 接收文件,保存到路径上
        /// </summary>
        /// <param name="filePath_assave">路径,将读取到的文件保存此路径。</param>
        public void cRecv_File(string filePath_assave)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 解析域名IP
        /// </summary>
        public string Get_DNS()
        {
            return Dns.GetHostAddresses("kv2010312.gicp.net")[0].ToString();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Client
{
    class cProgram
    {
        static void Main(string[] args)
        {
            CNTrans cnt = new CNTrans();
            cnt.cSet_ADDR(cnt.Get_DNS(), 1024, 4444);
            cnt.cSend_MSG("来自客户端的消息");

            Console.WriteLine(cnt.cRecv_MSG());
            Console.ReadKey();
        }
    }
}


服务器端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace Server
{
    class SNTrans
    {
        /// <summary>
        /// 端口
        /// </summary>
        private static  int sPort;
        /// <summary>
        /// 发送流的大小
        /// </summary>
        private static  int sDataSize;

        /// <summary>
        /// 设置服务器属性
        /// </summary>
        /// <param name="sport">开放的端口号</param>
        /// <param name="sdatasize">接收发送的流的大小</param>
        public void sSet_ADDR(int sport, int sdatasize)
        {
            sPort = sport;
            sDataSize = sdatasize;
        }

        /// <summary>
        /// 接收消息,并返回
        /// </summary>
        public TcpListener Listener;
        public void start()
        {
           Listener  = new TcpListener(IPAddress.Any, sPort);
            Listener.Start();
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        public string  sRecv_MSG()
        {
            Listener.Start();
            while (true)
            {
                //int bufferSize = sPort;//error!!!!!!!!!!!!!!!!!!!!!!!!!!!
                int bufferSize = sDataSize;
                TcpClient client = Listener.AcceptTcpClient();
                NetworkStream clientStream = client.GetStream();

                byte[] buffer = new byte[bufferSize];
                int readBytes = 0;
                readBytes = clientStream.Read(buffer, 0, bufferSize);

                clientStream.Close();
                return Encoding.GetEncoding("UTF-8").GetString(buffer);
            }
        }

        /// <summary>
        /// 接收消息,并返回。
        /// </summary>
        public bool  sSend_MSG(string message)
        {
            int bufferSize = sDataSize;
            TcpClient client = Listener.AcceptTcpClient();
            NetworkStream clientStream = client.GetStream();

            byte[] buffer = new byte[bufferSize];
            string request = Encoding.GetEncoding("UTF-8").GetString(buffer);

            byte[] s = Encoding.GetEncoding("UTF-8").GetBytes(message);
            clientStream.Write(s, 0, s.Length);

            clientStream.Close();
            return true;
        }

        /// <summary>
        /// 接收并保存文件
        /// </summary>
        /// <param name="filePath_ofsave">保存文件的路径</param>
        public void sRecv_File(string filePath_ofsave)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="filePath_ofsend">发送的文件的路径</param>
        public void sSend_File(string filePath_ofsend)
        {
            throw new System.NotImplementedException();
        }
        /// <summary>
        /// 解析域名IP
        /// </summary>
        public string Get_DNS()
        {
            return Dns.GetHostAddresses("kv2010312.gicp.net")[0].ToString();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Server
{
    class sProgram
    {
        static void Main(string[] args)
        {
            SNTrans snt = new SNTrans();
            Console.WriteLine(snt.Get_DNS());

            snt.sSet_ADDR(4444, 1024);
            snt.start();

            Console.WriteLine(snt.sRecv_MSG());

            snt.sSend_MSG("来至服务器的消息");
            Console.ReadKey();
        }
    }
}

各位高手,请你们告诉小弟,这是哪里出错了,为什么不能在两台机子上运行。

--------------------编程问答-------------------- 是什么错误?如果是连接错误,检查一下两台机器的防火墙设置. --------------------编程问答-------------------- 好好学习,天天向上...
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,