java socket编写服务器_Java网络学习笔记1:用(Server)Socket编写简单的客户/服务器程序...
首先需清楚Socket類提供了getInputStream()方法和getOutputStream()方法,分別返回輸入流InputStream對(duì)象和輸出流OutputStream對(duì)象。程序只需向數(shù)據(jù)流寫數(shù)據(jù),就能向?qū)Ψ桨l(fā)送數(shù)據(jù);只需從輸入流讀數(shù)據(jù),就能接收來自對(duì)方的數(shù)據(jù)。
創(chuàng)建服務(wù)器程序DemoServer01
public?class?DemoServer01?{
/**
*?@author?牧羊的伯格女皇??11/12
*?@param?args
*?創(chuàng)建服務(wù)端與客戶端之間的相互通信
*/
private?int?port?=?9999;
private?ServerSocket?server;
public?static?void?main(String[]?args)?{
new?DemoServer01().serverTalk();
}
public?DemoServer01(){
try?{
server?=new??ServerSocket(9999);
System.out.println("服務(wù)器啟動(dòng)且監(jiān)聽了端口:?"?+?server.getLocalPort()?);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
//輸出流
public?PrintWriter?getWriter(Socket?socket)?throws?IOException{
OutputStream?outSocket?=?socket.getOutputStream();
//參數(shù)true表示每寫一行,PrintWriter緩存就自動(dòng)溢出,把數(shù)據(jù)寫到目的地.
return?new?PrintWriter(?outSocket,?true?);
}
//?輸入流
public?BufferedReader?getReader(Socket?socket)?throws?IOException{
InputStream?inSocket?=?socket.getInputStream();
//?參數(shù)?Reader?????????????????????//參數(shù)?InputStream
return?new?BufferedReader(?new?InputStreamReader(?inSocket?)?);
}
public?void?serverTalk(){
Socket?socket?=?null;
try?{
socket?=?server.accept();??//一直偵聽等待客戶的連接
PrintWriter?pw?=?getWriter(socket);
BufferedReader?br?=?getReader(socket);
BufferedReader?localReader?=?new?BufferedReader(?new?InputStreamReader(?System.in?)?);
String?msg?=?null;
while(true){
while(?(msg?=?br.readLine()?)?!=?null?){
System.out.println("客戶端對(duì)服務(wù)端你說:?"?+?msg);
if(?msg.equals("bye")){
System.out.println("客戶端主動(dòng)斷開了與您的連接.");
break;
}
System.out.print("服務(wù)端對(duì)客戶端說:??"?);
String?inString?=?localReader.readLine();
pw.println(?inString?);
if(??inString.equals("bye")){
System.out.println("服務(wù)端斷開了與客戶端?"?+?socket.getInetAddress()?+"?的連接");
break;
}
}
}
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
if(?socket?!=?null?){
try?{
socket.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
}
對(duì)于服務(wù)器程序,其中最主要的方法還是為serverTalk()方法,它不斷等待客戶的連接請求,server.accept()方法返回一個(gè)Socket對(duì)象時(shí),就意味著與一個(gè)客戶建立了連接。接下來從Socket對(duì)象中得到輸出流和輸入流,并且分貝用PrintWriter和BufferedReader來裝飾他們。然后不斷調(diào)用BufferedReader 的 readLine()方法讀取客戶發(fā)來的字符串,在調(diào)用PrintWriter的println()方法向客戶返回字符串。如果為“bye”,則結(jié)束通信,調(diào)用socket.close()方法斷開連接。
3.創(chuàng)建客戶程序DemoClient01
public?class?DemoClient01?{
private?String?host?=?"localhost";
private?int?port?=?9999;
private?Socket?socket;
public?static?void?main(String[]?args)?{
new?DemoClient01().clientTalk();
}
public?DemoClient01(){
try{
socket?=?new?Socket(host,port);??//創(chuàng)建socket對(duì)象
System.out.println("客戶端已連接到服務(wù)器...");
}catch?(UnknownHostException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
public?PrintWriter?getWriter(Socket?socket)?throws?IOException{
OutputStream?outSocket?=?socket.getOutputStream();
return?new?PrintWriter(?outSocket,true);
}
public?BufferedReader?getReader(Socket?socket)?throws?IOException{
InputStream?inSocket?=?socket.getInputStream();
return?new?BufferedReader(?new?InputStreamReader(inSocket)?);
}
public?void?clientTalk(){
try?{
BufferedReader?br?=?getReader(socket);
PrintWriter?pw?=?getWriter(socket);
BufferedReader?localReader?=?new?BufferedReader(?new?InputStreamReader(System.in)?);
String?msg?=?null;
System.out.print("客戶端對(duì)服務(wù)端說:?");
while(?(msg?=?localReader.readLine()?)?!=?null?){
pw.println(msg);
if(?msg.equals(?"bye"?)){
System.out.println("客戶端主動(dòng)斷開與服務(wù)端的連接.");
break;
}
String?readString?=?br.readLine();
System.out.println("服務(wù)端對(duì)客戶端你說:?"?+?readString??);
if(?readString.equals(?"bye"?)){
System.out.println("服務(wù)端主動(dòng)斷開了與客戶端你的連接.");
break;
}
System.out.print("客戶端對(duì)服務(wù)端說:?");
}
}?catch?(UnknownHostException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}finally{
try?{
socket.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
對(duì)于客戶程序,其中最主要的是clientTalk()方法、其不斷讀取用戶從控制臺(tái)輸入的字符串,然后把它發(fā)送給服務(wù)器程序,再把服務(wù)器返回的字符串打印到控制臺(tái)。
總結(jié)
以上是生活随笔為你收集整理的java socket编写服务器_Java网络学习笔记1:用(Server)Socket编写简单的客户/服务器程序...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 打包zip下载_java we
- 下一篇: 从sqlserver中数据写入mysql