一个TCP的SERVER的程序遇到的问题(1分)

  • 主题发起人 主题发起人 bottom
  • 开始时间 开始时间
B

bottom

Unregistered / Unconfirmed
GUEST, unregistred user!
一个TCP的SERVER的程序遇到的问题<br><br>不过运行时出先RUNTIM的错误,大家帮忙看看如何修改啊?<br><br>program Project1;<br>{$APPTYPE CONSOLE} <br><br>uses &nbsp; Windows,winsock; <br><br>var<br>&nbsp; &nbsp;TempWSAData: TWSAData;<br>&nbsp; &nbsp;s: TSocket;<br>&nbsp; &nbsp;addr: TSockAddr;<br>&nbsp; &nbsp;RemoteAddr: PSockAddr;<br>&nbsp; &nbsp;SinSize: integer; <br><br>const<br>CRLF=#13#10;<br>UDPPORT = 6767; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//设定UDP端口号 <br><br>BEGIN<br>&nbsp; &nbsp; if WSAStartup($101, TempWSAData)=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp;Writeln('StartUp Error!'); <br><br>&nbsp; &nbsp; s := Socket(AF_INET, sock_stream, 0);<br>&nbsp; &nbsp; if (s = INVALID_SOCKET) then &nbsp; //Socket创建失败<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Writeln('Socket创建失败');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseSocket(s);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; //发送方SockAddr绑定<br>&nbsp; &nbsp; addr.sin_family := AF_INET;<br>&nbsp; &nbsp; addr.sin_addr.S_addr := INADDR_ANY;<br>&nbsp; &nbsp; addr.sin_port := htons(UDPPORT);<br>&nbsp; &nbsp; if Bind(s, addr, sizeof(addr)) &lt;&gt; 0 &nbsp;then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Writeln('bind fail');<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; //监听<br>&nbsp; &nbsp; write('listen = ');<br>&nbsp; &nbsp; listen(s,5);<br>&nbsp; &nbsp; Writeln(Listen(s,5));<br>&nbsp; &nbsp; write('accept = ');<br>&nbsp; &nbsp; New(RemoteAddr);<br>&nbsp; &nbsp; SinSize := SizeOf(RemoteAddr);<br>&nbsp; &nbsp; accept(s, RemoteAddr, PInteger(SinSize));<br>&nbsp; &nbsp; //write(accept(s, RemoteAddr, PInteger(SinSize)));<br>END. <br><br><br>&nbsp;<br>
 
哪里的 错误?<br>你也不标出来?
 
晕死,这个是我提在CSDN和S8S8。NET上的帖子<br>现在还没解决呢,帮我~~
 
&gt;&gt;UDPPORT = 6767; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//设定UDP端口号<br>&gt;&gt;s := Socket(AF_INET, sock_stream, 0);<br><br>不对呀,你上面是想用UDP,但下面创建Socket的时候又是用TCP。<br>s := socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); <br><br>如果是用TCP,那么改为<br>socket(AF_INET, Sock_Stream, IPProto_TCP);<br><br>还有,地址最好符合“网络字节顺序”:<br>ServerAddr.sin_addr.S_addr := htonl(INADDR_ANY);<br>
 

Similar threads

后退
顶部