日期:2014-05-19  浏览次数:20448 次

关于 Web Service 中的Socket的问题 急急急急!!求高手帮忙
我是菜鸟。今天遇到一个问题。
我有一个WinForm的Socket的例子,一个服务端一个客户端。
我把客户端改成了Web   Service   的形式,但是不能返回接过了。
Web   Service   的更改后代码如下
private   IPAddress   hostIPAddress;  
private   IPEndPoint   Server1;  
SqlConnection   strconn   =   new     SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "connectionstring "]);
private   Socket   sock;  
private   const   int   BufferSize=2048;  
private   byte[]   buffer=new   byte[BufferSize];  
private   static   ManualResetEvent   connectDone=new   ManualResetEvent(false);  
private   static   ManualResetEvent   sendDone=new   ManualResetEvent(false);
public   string   Receive= "没有接收 ";
public   string   Err= " ";
public   string   WZ= " ";

public   string   socket()
{
string   link=this.Link();
  }
//连接服务器
private   string   Link()  
{  
try  
{  
hostIPAddress=IPAddress.Parse( "127.0.0.1 ");  
}  
catch
{  
Err= "错误001:95 ";
return   "错误 ";
}  
try  
{  
Server1=new   IPEndPoint(hostIPAddress,Int32.Parse( "7000 "));  
sock=new   Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);  
sock.BeginConnect(Server1,new   AsyncCallback(this.ConnectCallBack),sock);
}  
catch  
{
Err= "错误002:103 ";
return   "错误 ";
}  
return   "OK ";
}  
//连接函数
public   void   ConnectCallBack(IAsyncResult   ar)  
{  
try  
{  
Socket   client=(Socket)ar.AsyncState;   //获取状态  
client.EndConnect(ar);  
try  
{  
byte[]   byteData=Encoding.ASCII.GetBytes( "0015001000010100 ");  
sock.BeginSend(byteData,0,byteData.Length,0,new   AsyncCallback(SendCallBack),sock);  
}  
catch
{
Err= "错误003:128 ";//return   "错误 ";
}  

Receive= "与主机127.0.0.1端口7000连接成功 ";  
Thread   thread=new   Thread(new   ThreadStart(ThreadProc));  
thread.Start();   //开始接收数据线程  
connectDone.Set();   //将指定事件的状态设置为终止。  
}  
catch
{
Err= "错误004:139 ";//return   "错误 ";
}   // return   "OK ";
}  

这是部分代码;
ConnectCallBack()   函数好像被调用了,但是好像调用了部分。


byte[]   byteData=Encoding.ASCII.GetBytes( "0015001000010100 ");  
sock.BeginSend(byteData,0,byteData.Length,0,new   AsyncCallback(SendCallBack),sock);  
这些起到作用了   下面那些就没有起到作用:
Receive= "与主机127.0.0.1端口7000连接成功 ";  
那位高手教教小弟,实在不知道怎么回事。拜谢了!
我的QQ148390950   注明   csdn
吃完饭回来看帖。拜谢了!




------解决方案--------------------
帮顶。