如何使用Socket API函数来连接服务端、发送消息(100分)

  • 主题发起人 主题发起人 okla
  • 开始时间 开始时间
O

okla

Unregistered / Unconfirmed
GUEST, unregistred user!
如果调用API函数连接Socket服务器并发消息该怎么写?我不想用ClientSocket构件。<br><br>另外如果使用ClientSocket构件,我在DLL中用ClientSocket构件连接Socket服务器,可调<br>用DLL后连接时总出错,而设计期时又能连接成功,怎么回事?我放了一个<br>TCustomerData在DLL中,ClientSocket就放在CustomerData中。<br>
 
这样应该没有什么问题啊,奇怪。<br>其实也没有什么难的。<br>如果是tcp:<br>socket--&gt;bind--&gt;connect--&gt;recv--&gt;send<br>如果是ucp:<br>socket--&gt;bind--&gt;send--&gt;recv
 
你到底在问什么问题?<br>socket的设计步骤都是有规范的,找个离子看看不就行了。
 
&lt;p align="left"&gt;<br>&lt;font color="#FF0000"&gt;<br>大家帮帮忙,给看一下&lt;/font&gt;&lt;/p&gt;<br><br>&lt;p align="left"&gt;<br>&amp;nbsp;<br>&lt;font color="#FF0000" size="4"&gt;<br>&lt;a href="http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=506445"&gt;<br>“寻找简单的方法,filter消息”&lt;/a&gt;<br>&lt;/font&gt;&lt;/p&gt;<br><br>&lt;p align="left"&gt;如有违反论坛规则,还请版主砍掉并批评指出&lt;/p&gt;
 
用WINSOCKS API 函数进行通讯。函数功能详见DELPHI帮助。<br>unit <br>uses<br>&nbsp;winsock,...;//winsock单元可在delphi目录中找到。<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; xl_socket:Tsocket;<br><br>implementation<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>VAR<br>XL_VERSION:WORD;<br>XL_WSADATA:tWSADATA;<br>RET:INTEGER;<br>hostname:string;<br>server_port:integer;<br>xl_hostent:phostent;<br>xl_sockaddrin:TSOCKADDRIN;<br>psaddr:^longint;<br>saddr:integer;<br>begin<br>&nbsp; &nbsp; &nbsp;server_port:=8080;<br>&nbsp; &nbsp; &nbsp;hostname:='111.111.111.111';<br>&nbsp; &nbsp; &nbsp;xl_sockaddrin.sin_port:=htons(server_port);<br>&nbsp; &nbsp; &nbsp;xl_sockaddrin.sin_family:=PF_INET;<br>&nbsp; &nbsp; &nbsp;xl_hostent:=gethostbyname(PCHAR(HOSTNAME));<br>&nbsp; &nbsp; &nbsp;IF xl_hostent=nil then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saddr:=inet_addr(pchar(hostname));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if saddr&lt;&gt;-1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xl_sockaddrin.sin_addr.S_addr:= saddr;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;ELSE<br>&nbsp; &nbsp; &nbsp;BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; psaddr:=pointer(xl_hostent.h_addr_list^);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xl_sockaddrin.sin_addr.S_addr:=psaddr^;<br>&nbsp; &nbsp; &nbsp;END;<br>&nbsp; &nbsp; &nbsp;XL_VERSION:=$101;<br>&nbsp; &nbsp; &nbsp;RET:=WSASTARTUP(XL_VERSION,XL_WSADATA);<br>&nbsp; &nbsp; &nbsp;IF RET=0 THEN<br>&nbsp; &nbsp; &nbsp;BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APPLICATION.MessageBox(初始化成功','注意',mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xl_socket:=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF XL_SOCKET=INVALID_SOCKET THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;APPLICATION.MessageBox('SOCKET创建失败','注意',mb_ok)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;APPLICATION.MessageBox('SOCKET创建成功','注意',mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RET:=CONNECT(xl_socket,xl_sockaddrin,sizeof(xl_sockaddrin));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ret=socket_error then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;APPLICATION.MessageBox('SOCKET连接失败','注意',mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret:=closesocket(xl_socket);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ret=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; APPLICATION.MessageBox('SOCKET释放成功!','注意',mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;APPLICATION.MessageBox('SOCKET连接成功!','注意',mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;button2.Enabled:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;button3.Enabled:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WSAcleanup;<br>&nbsp; &nbsp; &nbsp;END;<br><br>procedure TForm1.Button3Click(Sender: TObject);//发送<br>var<br>ret,len:integer;<br>s:string;<br>psend:pchar;<br>begin<br>&nbsp; &nbsp; &nbsp; s:=memo1.text;<br>&nbsp; &nbsp; &nbsp; len:=length(s);<br>&nbsp; &nbsp; &nbsp; Psend:=stralloc(LEN);<br>&nbsp; &nbsp; &nbsp; STRPCOPY(Psend,S);<br>&nbsp; &nbsp; &nbsp; ret:=send(xl_socket,Psend^,len,0);<br>&nbsp; &nbsp; &nbsp; if ret&lt;&gt;SOCKET_ERROR then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application.MessageBox('发送成功','注意',mb_ok)<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application.MessageBox('发送失败','注意',mb_ok);<br>end;<br><br>
 
这是服务器接受客户信息,再反送回去<br><br>program server1;<br><br>uses windows,winsock;<br><br>const myport=1414;//预定义口<br><br>{type<br> sockaddr_in = record<br>  case Integer of<br>   0: (sin_family: u_short;<br>     sin_port: u_short;<br>     sin_addr: TInAddr;<br>     sin_zero: array[0..7] of Char);<br>   1: (sa_family: u_short;<br>     sa_data: array[0..13] of Char)<br> end;<br> TSockAddrIn = sockaddr_in;<br> PSOCKADDR = ^TSockAddr;<br> TSockAddr = sockaddr_in;<br><br> WSAData = record // !!! also WSDATA<br>  wVersion: Word;<br>  wHighVersion: Word;<br>  szDescription: array[0..WSADESCRIPTION_LEN] of Char;<br>  szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;<br>  iMaxSockets: Word;<br>  iMaxUdpDg: Word;<br>  lpVendorInfo: PChar;<br> end;<br> TWSAData = WSAData;<br><br>end<br>}<br><br>var str:string;<br>  s,ns:TSocket;//type:u_int|integer;<br>  ver:word;<br>  rece:TWSAData;//type:WSAData;<br>  wsstatus:integer;//winsocket_status;<br>  i,pktlen:integer;<br>  szd:array[0..WSADESCRIPTION_LEN] of Char;<br>  szs:array[0..WSASYS_STATUS_LEN] of Char;<br>  server:TSockAddr;//tpye:sockaddr_in<br>  client:PSockAddr;//type:^sockaddr_in<br>  namelen:PInteger;//type:^Integer;<br>  buf:array[0..1023] of char;<br><br>begin<br>ver:=514;<br>write('WSAStartup = ');<br>try<br>wsstatus:=WSAStartup(ver,rece);<br>//初始化winsock;在windows下是必须的过程<br>writeln(wsstatus);//winsock状态,0代表成功<br>if wsstatus=0 then<br>begin<br>writeln;<br>writeln('********************************************************************');<br>//以下显示winsock状态<br>write('wVersion = ');<br>writeln(rece.wVersion);<br>write('wHighVersion = ');<br>writeln(rece.wHighVersion);<br>write('iMaxSockets = ');<br>writeln(rece.iMaxSockets);<br>write('iMaxUdpDg = ');<br>writeln(rece.iMaxUdpDg);<br>writeln('szDescription is ');<br>for i:=0 to WSADESCRIPTION_LEN do write(szd);<br>writeln;<br>writeln('szSystemStatus is ');<br>for i:=0 to WSASYS_STATUS_LEN do write(szs);<br>writeln;<br>writeln('********************************************************************');<br>end;<br><br>write('socket = ');<br>s:=socket(AF_INET,SOCK_STREAM,0);<br>//第一步,建立服务器的socket,0表示成功<br>writeln(s);<br>if s&lt;0 then exit;<br><br>write('bind = ');<br>server.sin_family := AF_INET;<br>server.sin_port := htons(myport);<br>server.sin_addr.s_addr := INADDR_ANY;<br>wsstatus:=bind(s,server,sizeof(server));<br>//第二步,绑定,0表示成功<br>writeln(wsstatus);<br>if wsstatus&lt;&gt;0 then exit;<br><br>write('listen = ');<br>wsstatus:=listen(s,5);<br>//第三步,监听<br>writeln(wsstatus);<br>if wsstatus&lt;&gt;0 then exit;<br><br>write('accept = ');<br>new(client);<br>new(namelen);<br>namelen^:=sizeof(client^);<br>ns:=accept(s,client,namelen);<br>//第四步,等待接收<br>//注:bind时传递的是sockaddr_in结构的数据,accept时是两个指针!!!<br>if ns=-1 then //以下可以做成多线程过程<br>  begin<br>  writeln(-1);<br>  exit;<br>  end<br>  else<br>  begin<br>  writeln(ns);<br>  pktlen:=0;<br>  writeln('recv');<br>  repeat<br>  pktlen:= recv(ns,buf,1024,0);//接收数据<br>//pktlen=为接收长度,0表示断开联接,&lt;0表示失败<br>  sleep(1);<br>  write(pktlen:5);<br>  if pktlen&gt;0 then<br>  begin<br>  i:=send(ns,buf,pktlen,0);//返回数据<br>  end;<br>  until pktlen&lt;1;//循环直到客户断开联接<br>  end;<br>finally<br>writeln;<br>writeln('******************************************************************');<br>writeln('Press to exit winsocket');<br>readln(str);<br>wsstatus:=WSACleanup();//退出winsocket;<br>write('WSACleanup = ');<br>writeln(wsstatus);<br>end;<br>writeln;<br>writeln('Press to exit');<br>readln(str);<br>end.
 
多人接受答案了。
 
TO fu_xiang_yu:<br>怎样做成多线程过程,能否发源码给我?<br>email:zjl4507@sina.com<br><br>
 
后退
顶部