資源簡介
要求客戶機可以向服務器傳送數據,服務器從客戶端中讀取數據。實際上,服務器也可以向客戶機發送數據,只要像客戶機那樣建立輸出流即可;客戶機也可以接受服務器的數據,只要像服務器那樣建立連接即可。
代碼片段和文件信息
package?pack;
import?java.net.*;
import?java.io.*;
public?class?TCPClient?{
public?static?void?main(String?args[])?throws?UnknownHostException
{
//爭論供應信息和目的地的?hostname
Socket?s=null;
InetAddress?addr=InetAddress.getByName(“192.168.3.9“);
try{
int?serverPort=7896;
s=new?Socket(addrserverPort);
DataInputStream?in=new?DataInputStream(s.getInputStream());
DataOutputStream?out=new?DataOutputStream(s.getOutputStream());
out.writeUTF(args[0]);???//UTF?is?a?string?encoding?see?Sn?4.3
String?data=in.readUTF();
System.out.println(“Received:“+data);
}
catch(UnknownHostException?e)
{
System.out.println(“Sock:“+e.getMessage());
}
catch(EOFException?e){System.out.println(“EOF:“+e.getMessage());}
catch(IOException?e){System.out.println(“IO:“+e.getMessage());}
finally{if(s!=null)??try{s.close();}?catch(IOException?e){/*close?failed*/}}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????385??2009-06-12?00:53??TCP通信\.project
?????文件????????226??2009-06-12?00:53??TCP通信\.classpath
?????文件????????930??2009-06-12?02:26??TCP通信\pack\TCPClient.java
?????文件???????1965??2009-06-12?02:26??TCP通信\pack\TCPClient.class
?????文件???????1192??2009-06-12?02:08??TCP通信\pack\TCPServer.java
?????文件???????1109??2009-06-12?02:08??TCP通信\pack\TCPServer.class
?????文件???????1653??2009-06-12?02:08??TCP通信\pack\Connection.class
?????目錄??????????0??2009-06-12?00:55??TCP通信\pack
?????目錄??????????0??2009-06-12?00:53??TCP通信
-----------?---------??----------?-----??----
?????????????????7460????????????????????9
評論
共有 條評論