這篇文章主要介紹了如何在java中使用網(wǎng)絡(luò)通信技術(shù)實(shí)現(xiàn)聊天小程序的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇如何在java中使用網(wǎng)絡(luò)通信技術(shù)實(shí)現(xiàn)聊天小程序文章都會有所收獲,下面我們一起來看看吧。

首先是服務(wù)端代碼:
package ChatTwoPackage;
import java.io.*;
import java.net.*;
public class ChatTwoServer {
public ChatTwoServer(int port,String name) throws IOException
{
ServerSocket server=new ServerSocket(port);//創(chuàng)建seversocket對象,提供tcp連接服務(wù)。指定端口port,等待tcp連接。
System.out.print("正在等待連接,請勿操作!");
Socket client=server.accept();//創(chuàng)建socket對象,它等待接收客戶端的連接。
new ChatTwoClient(name,client);//實(shí)現(xiàn)圖形界面。
server.close();
}
public static void main(String[] args) throws IOException {
new ChatTwoServer(2001,"SQ");
}
}然后是客戶端的代碼:
package ChatTwoPackage;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ChatTwoClient extends JFrame implements ActionListener {
private String name;
private JTextArea text_re;
private JTextField text_se;
private PrintWriter cout;
private JButton buttons[];
public ChatTwoClient(String name,Socket socket) throws IOException
{
super("我:"+name+InetAddress.getLocalHost().getHostAddress()+":"+socket.getLocalPort());
this.setBounds(320, 240, 400, 240);
this.text_re=new JTextArea();
this.text_re.setEditable(false);
this.getContentPane().add(new JScrollPane(this.text_re));
JToolBar toolBar=new JToolBar();
this.getContentPane().add(toolBar,"South");
toolBar.add(this.text_se=new JTextField(30));
buttons=new JButton[2];
buttons[0]=new JButton("發(fā)送");
buttons[1]=new JButton("下線");
toolBar.add(buttons[0]);
toolBar.add(buttons[1]);
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);//給按鈕添加事件監(jiān)聽,委托當(dāng)前對象處理
this.setVisible(true);
this.name=name;
this.cout=new PrintWriter(socket.getOutputStream(),true);//獲得socket輸出流
this.cout.println(name);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); //將socket的字節(jié)輸入流轉(zhuǎn)換為字符流,默認(rèn)GBK字符集,再創(chuàng)建緩沖字符輸入流
String line="連接"+br.readLine()+"成功";
while(line!=null&&!line.endsWith("bye"))
{
text_re.append(line+"\r\n");
line=br.readLine();
}//讀取對方發(fā)送的內(nèi)容并顯示,直到內(nèi)容為為空或?qū)Ψ较戮€
br.close();
this.cout.close();
socket.close();
buttons[0].setEnabled(false);
buttons[1].setEnabled(false);
}
public ChatTwoClient(String name,String host,int port) throws IOException
{
this(name,new Socket(host,port));//調(diào)用另一個(gè)構(gòu)造方法
}
public void actionPerformed(ActionEvent ev)
{
if(ev.getActionCommand().equals("發(fā)送"))
{
this.cout.println(name+":"+text_se.getText());
text_re.append("我:"+text_se.getText()+"\n");
text_se.setText("");
}//按下發(fā)送按鈕后,將內(nèi)容發(fā)出,并更新自己聊天框的內(nèi)容
if(ev.getActionCommand().equals("下線"))
{
text_re.append("你已下線\n");
this.cout.println(name+"離線\n"+"bye\n");
buttons[0].setEnabled(false);
buttons[1].setEnabled(false);
}//下線按鈕按下后,發(fā)送bye作為下線標(biāo)記
}
public static void main(String[] args) throws IOException {
new ChatTwoClient("mxl","127.0.0.1",2001); //ip地址和端口
}
}運(yùn)行效果:

說明:
1.兩臺計(jì)算機(jī)一臺作為服務(wù)端,作為服務(wù)端的計(jì)算機(jī)需要有兩個(gè)代碼。首先運(yùn)行服務(wù)端的代碼,等待客戶端機(jī)器連接,客戶端運(yùn)行客戶端代碼后,提示連接成功。就可以發(fā)送信息了。
2.運(yùn)行代碼前需要將ip地址改為自己計(jì)算機(jī)當(dāng)前的ip地址(Modem、ISDN、ADSL、有線寬頻、小區(qū)寬頻等方式上網(wǎng)的計(jì)算機(jī),每次上網(wǎng)所分配到的IP地址都不相同,這稱為動態(tài)IP地址)。如果要用一臺計(jì)算機(jī)充當(dāng)客戶端和服務(wù)端,就將ip地址寫為:127.0.0.1(127.0.0.1是回送地址,指本地機(jī),一般用來測試使用)。先運(yùn)行服務(wù)端代碼,再運(yùn)行客戶端代碼即可。
關(guān)于“如何在java中使用網(wǎng)絡(luò)通信技術(shù)實(shí)現(xiàn)聊天小程序”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“如何在java中使用網(wǎng)絡(luò)通信技術(shù)實(shí)現(xiàn)聊天小程序”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:如何在java中使用網(wǎng)絡(luò)通信技術(shù)實(shí)現(xiàn)聊天小程序-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.chinadenli.net/article10/dodedo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、軟件開發(fā)、定制網(wǎng)站、服務(wù)器托管、移動網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容