日期:2014-05-16  浏览次数:20610 次

Socket 向服务器发送图片 图片是损坏的,如下源码所示 ,求教


Socket 向服务器发送图片 图片是损坏的 ,找不出原因,不知是不是不能添加报头字符,请高手求教,

服务端

public class SocketServer
    {
        Socket sRecvPic;//接收图片的socket
        int recvPicPort;//接收图片端口        

        //建立本地socket,一直对1234端口进行侦听   
        public SocketServer(int recvPort)
        {
            recvPicPort = recvPort;
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, recvPicPort);
            sRecvPic = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sRecvPic.Bind(localEndPoint);
            sRecvPic.Listen(100);
        }

        public void thread()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(1);//每个线程内部的死循环里面都要加个“短时间”睡眠,使得线程占用资源得到及时释放
                try
                {
                    Socket sRecvPicTemp = sRecvPic.Accept();//一直在等待socket请求,并建立一个和请求相同的socket,覆盖掉原来的socket                   
                    sRecvPicTemp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000); //设置接收数据超时                    
                    sRecvPicTemp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000);//设置发送数据超时                    
                    sRecvPicTemp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1024000);//设置发送缓冲区大小--1K大小                   
                    sRecvPicTemp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1024000); //设置接收缓冲区大小
         &n