请教TCP连接问题(50分)

  • 主题发起人 主题发起人 hx123123_2000
  • 开始时间 开始时间
H

hx123123_2000

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,小弟写了个TCP程序,想在两台机器上传输字符串.一台机子是PC机,用的是DELPHI,一台是SUN工作站,用的是C++,PC传过去的字符串,SUN工作站能收到,但它发给我的我收不到,下面是我的程序,请大家指点一下.<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> send:string;<br>begin<br>IdTCPServer1.DefaultPort:=6000;<br>IdTCPServer1.Active:=true;<br>IdTCPClient1.Port:=9000;<br>IdTCPClient1.Host:='192.168.1.4';<br>IdTCPClient1.Connect;<br>IdTCPClient1.Write(send);<br>end;<br>procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);<br>var<br> get:string;<br>begin<br> &nbsp; if not AThread.Terminated and AThread.Connection.Connected then<br> &nbsp; begin<br> &nbsp; &nbsp; get:=AThread.Connection.ReadLn(#13,-1,-1);<br> &nbsp; &nbsp; Memo1.Lines.Add(get);<br> &nbsp; end;<br>end;<br>请大家多多指点
 
建议你用API来写吧!只是发送一个字符串而已,很简单的!
 
呵呵,就是不会写,所以才用的控件.因为没有时间了,要交东西了。fxh7622请问你有现成的程序吗?
 
现在写winsock更倾向于用API自己写,控制上更自由一些。<br><br>给你一个小Demo(C)看是否你想要的呢:<br><br>delphi中,你自己uses Winsock,就可以改写过来了<br>--------------------------------------<br>编译环境: C++ Builder6.0<br><br>//---------------------------------------------------------------------------------------------<br>//公共单元<br>//common.h<br>#ifndef commonH<br>#define commonH<br>#endif<br><br>#define SERVER_PORT &nbsp;21<br>#define SERVER_IP &nbsp; &nbsp;&quot;127.0.0.1&quot;<br>#define MAX_SOCKFD &nbsp; 8<br><br>void InitWinSock();<br>void UnInitWinSock();<br><br>//common.cpp<br>#pragma hdrstop<br><br>#include &lt;common.h&gt;<br>#include &lt;winsock.h&gt;<br>#include &lt;windows.h&gt;<br><br>#pragma package(smart_init)<br><br><br>void InitWinSock()<br>{<br> &nbsp;WSADATA wsaData;<br><br> &nbsp;WSAStartup(MAKEWORD(2, 2), &wsaData);<br>}<br><br>void UnInitWinSock()<br>{<br> &nbsp;WSACleanup;<br>}<br><br>//---------------------------------------------------------------------------------------------<br>//服务端<br>//tcp_server.cpp<br>#pragma hdrstop<br>#include &lt;stdio.h&gt;<br>#include &lt;inetsdk.h&gt;<br>#include &lt;wininet.h&gt;<br>#include &lt;winsock.h&gt;<br>#include &lt;iostream&gt;<br>#include &lt;windows.h&gt;<br>#include &lt;io.h&gt;<br>#include &lt;sys/types.h&gt;<br>#include &lt;common.h&gt; <br><br>#pragma argsused<br><br><br>int main(int argc, char* argv[])<br>{<br> &nbsp;int fd_listen,fd_accept,client[MAX_SOCKFD],fd,reclen,i,n;<br> &nbsp;int maxfd,maxi,nready;<br> &nbsp;int &nbsp;optlen=sizeof('1');<br> &nbsp;char optval='1';<br> &nbsp;int addr_len=sizeof(struct sockaddr_in);<br> &nbsp;char buff[256];<br> &nbsp;char msg[]=&quot;welcome to server!&quot;;<br> &nbsp;char msg2[]=&quot;too many clients!/n&quot;;<br> &nbsp;struct sockaddr_in sock_server,sock_client;<br> &nbsp;fd_set readfds,ifds;<br><br> &nbsp;InitWinSock();<br> &nbsp;fd_listen=socket(AF_INET,SOCK_STREAM,0);<br> &nbsp;if (fd_listen&lt;0) {<br> &nbsp; &nbsp; printf(&quot;socket error&quot;);<br> &nbsp; &nbsp; exit(1);<br> &nbsp; &nbsp; }<br> &nbsp;/*填写 sockaddr_in 结构*/<br> &nbsp;memset(&sock_server,0,addr_len);<br> &nbsp;sock_server.sin_family =AF_INET;<br> &nbsp;sock_server.sin_port =htons(SERVER_PORT);<br> &nbsp;sock_server.sin_addr.S_un.S_addr =htonl(INADDR_ANY);<br><br> &nbsp;/* 如果服务器终止后,服务器可以第二次快速启动而不用等待一段时间 &nbsp;*/<br> &nbsp;setsockopt(fd_listen,SOL_SOCKET,SO_REUSEADDR,&optval,optlen);<br><br> &nbsp;/*使用bind()把上面的socket和定义的IP地址和端口绑定*/<br> &nbsp;if (bind(fd_listen,(struct sockaddr*)(&sock_server),addr_len)&lt;0) {<br> &nbsp; &nbsp; printf(&quot;socket bind error!/n&quot;);<br> &nbsp; &nbsp; exit(1);<br> &nbsp; &nbsp; }<br><br> &nbsp;/*使用listen()来使socket接受进入的连接请求,<br> &nbsp; *第二个参数为socket可维护的最大连接数(包括已连接的和请求连接的数量)。<br> &nbsp; */<br> &nbsp;if (listen(fd_listen,MAX_SOCKFD)&lt;0) {<br> &nbsp; &nbsp; printf(&quot;socket listen error!/n&quot;) ;<br> &nbsp; &nbsp; exit(1);<br> &nbsp; &nbsp; }<br><br> &nbsp;/* initialize */<br> &nbsp;maxfd = fd_listen;<br> &nbsp;maxi=-1;<br> &nbsp;fd_accept=0;<br> &nbsp;for (fd=0;fd&lt;MAX_SOCKFD;fd++)<br> &nbsp; &nbsp;client[fd]=-1; &nbsp;/* -1 indicates available entry */<br> &nbsp;FD_ZERO(&readfds);<br> &nbsp;FD_SET(fd_listen,&readfds);<br><br> &nbsp;while (1) {<br> &nbsp; &nbsp;//--------------------------------------------------------------------------<br> &nbsp; &nbsp;ifds=readfds;<br> &nbsp; &nbsp;nready=select(maxfd + 1, &ifds, NULL, NULL, NULL);<br><br> &nbsp; &nbsp;if (FD_ISSET(fd_listen, &ifds)) { /* new client connection */<br> &nbsp; &nbsp; &nbsp; //新的连接<br> &nbsp; &nbsp; &nbsp; fd_accept=accept(fd_listen,(struct sockaddr*)(&sock_client),&addr_len);<br><br> &nbsp; &nbsp; &nbsp; if (fd_accept&gt;0) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sendto(fd_accept,msg,sizeof(msg),0,(struct sockaddr*)(&sock_client),addr_len);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;connect from %s/n&quot;,inet_ntoa(sock_client.sin_addr));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; for (i=0;i&lt;MAX_SOCKFD;i++)<br> &nbsp; &nbsp; &nbsp; &nbsp;if (client&lt;0){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client =fd_accept;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} //for i<br><br> &nbsp; &nbsp; &nbsp; //是否超出最大连接数<br> &nbsp; &nbsp; &nbsp; if (i==MAX_SOCKFD) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sendto(fd_accept,msg2,sizeof(msg2),0,(struct sockaddr*)(&sock_client),addr_len);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//write(fd_accept, msg2, sizeof(msg2));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;close(fd_accept);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; FD_SET(fd_accept, &readfds); /* add new descriptor to set */<br><br> &nbsp; &nbsp; &nbsp; if (fd_accept&gt;maxfd) maxfd=fd_accept;<br><br> &nbsp; &nbsp; &nbsp; if (i&gt;maxi) maxi=i;<br><br> &nbsp; &nbsp; &nbsp; if (--nready&lt;=0) continue; /* no more readable descriptors */<br> &nbsp; &nbsp; &nbsp; }//if<br><br> &nbsp; &nbsp; //==============================================<br> &nbsp; &nbsp; for (i = 0; i &lt;= maxi; i++) { /* check all clients for data */<br> &nbsp; &nbsp; &nbsp; &nbsp;if ((fd = client) &lt; 0) continue;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if (FD_ISSET(fd, &ifds)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset(buff,0,sizeof(buff));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reclen=recvfrom(fd,buff,sizeof(buff),0,(struct sockaddr*)(&sock_server),&addr_len);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (reclen&lt;=0){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*连接中断,清除连接状态旗标*/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;connection close./n&quot;);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client = -1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FD_CLR(fd, &readfds); /* turn off the bit for sockfd in allset */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;close(fd);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;receive message from client: %s/n&quot;,buff);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//返回一条确认信息<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sendto(fd,buff,sizeof(buff),0,(struct sockaddr*)(&sock_server),addr_len);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (--nready &lt;= 0) break; /* no more readable descriptors */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } //if 1<br><br> &nbsp; &nbsp; &nbsp; &nbsp;} //end for i<br> &nbsp; &nbsp; //==============================================<br><br> &nbsp; &nbsp;//--------------------------------------------------------------------------<br> &nbsp; &nbsp;} /*while*/<br><br> &nbsp;close(fd_listen);<br> &nbsp;UnInitWinSock();<br> &nbsp;return 0;<br>}<br><br>//---------------------------------------------------------------------------------------------------<br>//客户端<br>//tcp_client.cpp<br>#pragma hdrstop<br><br>#include &lt;stdio.h&gt;<br>#include &lt;inetsdk.h&gt;<br>#include &lt;wininet.h&gt;<br>#include &lt;winsock.h&gt;<br>#include &lt;iostream&gt;<br>#include &lt;io.h&gt;<br>#include &lt;windows.h&gt;<br>#include &lt;sys/types.h&gt;<br>#include &lt;common.h&gt;<br><br><br><br>#pragma argsused<br>int main(int argc, char* argv[])<br>{<br> &nbsp;int sockfd;<br> &nbsp;int addr_len=sizeof(struct sockaddr_in);<br> &nbsp;int reclen;<br> &nbsp;char buff[256];<br> &nbsp;struct sockaddr_in sock_client;<br><br> &nbsp;InitWinSock();<br> &nbsp;sockfd=socket(AF_INET,SOCK_STREAM,0);<br> &nbsp;if (sockfd&lt;0)<br> &nbsp;{<br> &nbsp; printf(&quot;socket error&quot;);<br> &nbsp; exit(1);<br> &nbsp; }<br><br> &nbsp;/*填写 sockaddr_in 结构*/<br> &nbsp;memset(&sock_client,0,addr_len);<br> &nbsp;sock_client.sin_family =AF_INET;<br> &nbsp;sock_client.sin_port =htons(SERVER_PORT);<br> &nbsp;sock_client.sin_addr.S_un.S_addr =inet_addr(SERVER_IP);<br><br> &nbsp;/*连接到server*/<br> &nbsp;if (connect(sockfd,(struct sockaddr*)(&sock_client),addr_len)&lt;0)<br> &nbsp;{<br> &nbsp; printf(&quot;socket connect error!/n&quot;);<br> &nbsp; exit(1);<br> &nbsp; }<br><br> &nbsp;/*接收由server端传来的信息*/<br> &nbsp;recv(sockfd,buff,sizeof(buff),0);<br> &nbsp;printf(&quot;receive message from server: %s/n&quot;,buff);<br><br> &nbsp;while(1) {<br> &nbsp; &nbsp;memset(buff,0,sizeof(buff));<br> &nbsp; &nbsp;/*从标准输入设备取得字符串*/<br> &nbsp; &nbsp;printf(&quot;please input one string to send : &quot;);<br> &nbsp; &nbsp;scanf(&quot;%s&quot;,buff); <br><br> &nbsp; &nbsp;if (send(sockfd,buff,sizeof(buff),0)&lt;0) {<br> &nbsp; &nbsp; &nbsp; printf(&quot;socket send error!/n&quot;);<br> &nbsp; &nbsp; &nbsp; exit(1);<br> &nbsp; &nbsp; &nbsp; }<br><br> &nbsp; &nbsp;memset(buff,0,sizeof(buff));<br> &nbsp; &nbsp;reclen=recv(sockfd,buff,sizeof(buff),0);<br> &nbsp; &nbsp;if (reclen&gt;0)<br> &nbsp; &nbsp; &nbsp; printf(&quot;receive message from server: %s/n/n&quot;,buff);<br><br> &nbsp; &nbsp;}//while<br><br> &nbsp;close(sockfd);<br><br> &nbsp;UnInitWinSock();<br> &nbsp;return 0;<br>}<br><br>//-----------------------------------------------------------------------------
 
55555,谢谢你的答案.但确实看的不怎么明白.对C不熟,更别提转换过来了。<br>现在我的问题是我能给用C语言写的服务器端发,它也能收到.但它发给我的,我的服务器端却收不到,但我自己给自己发的却能收到,可能错在哪里了???
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
758
import
I
I
回复
0
查看
820
import
I
后退
顶部