首先確定兩臺(tái)電腦都在同一個(gè)局域網(wǎng)。兩臺(tái)電腦都要裝有java的運(yùn)行環(huán)境,(直接兩臺(tái)電腦都裝jdk吧 ,jre也是可以的)把代碼分別拷貝到兩臺(tái)電腦上,假如用你的電腦做服務(wù)器,另一臺(tái)電腦做客戶端,先起你電腦的服務(wù)器,再在你電腦起一個(gè)客戶端,然后在另一臺(tái)電腦上再起一個(gè)客戶端,這樣兩臺(tái)電腦的客戶端就可以通訊了。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供樂(lè)安網(wǎng)站建設(shè)、樂(lè)安做網(wǎng)站、樂(lè)安網(wǎng)站設(shè)計(jì)、樂(lè)安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、樂(lè)安企業(yè)網(wǎng)站模板建站服務(wù),十多年樂(lè)安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
注意:另一臺(tái)電腦代碼上面的IP地址要正確真上你電腦的IP地址
不懂的再繼續(xù)問(wèn)
import java.io.*;
import java.util.*;
import java.net.InetAddress;
public class Ip{
static public HashMap ping; //ping 后的結(jié)果集
public HashMap getPing(){ //用來(lái)得到ping后的結(jié)果集
return ping;
}
//當(dāng)前線程的數(shù)量, 防止過(guò)多線程摧毀電腦
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30個(gè)線程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本機(jī)的IP,得到網(wǎng)段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){ //對(duì)所有局域網(wǎng)Ip
String iip=ss+i;
Ping(iip);
}
//等著所有Ping結(jié)束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
if(value.equals("true"))
System.out.println(key+"--"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}
public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//讀取結(jié)果行
for (int i=1 ; i 7; i++)
input.readLine();
String line= input.readLine();
if (line.length() 17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//線程結(jié)束
threadCount -= 1;
}catch (IOException e){}
}
}
}
在知道里發(fā)代碼,發(fā)截圖就好,沒(méi)必要全貼上來(lái)。
局域網(wǎng)聊天的程序原理:
1、在同一個(gè)網(wǎng)絡(luò)。
2、有臨時(shí)或者永久數(shù)據(jù)庫(kù),進(jìn)行數(shù)據(jù)傳輸與訪問(wèn)。
3、有單獨(dú)服務(wù)器或者用戶服務(wù)器。
至于具體的學(xué)習(xí),我給你個(gè)建議吧。
一步一步,照著代碼寫(xiě),你邊寫(xiě),你就會(huì)報(bào)錯(cuò),通過(guò)解決錯(cuò)誤,你就會(huì)很快學(xué)會(huì)了。
祝樓主學(xué)習(xí)愉快~
//以下引入包
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class regit extends JApplet implements ActionListener,ItemListener//接口
{
String s=null;
//對(duì)象的聲明
JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//標(biāo)簽
JTextField textname,textaddress,textmail,textphone;//文本框
JRadioButton r1,r2;//單選按紐
ButtonGroup bg;//組
JPasswordField textpass;//密碼域
JButton buttonregit,buttonreset;//注冊(cè)按紐 重寫(xiě)按紐
JPanel p;//面板
String sex;//定義性別字符串
URL url;//統(tǒng)一資源定位
BufferedWriter out1,out2;//流
BufferedReader in;
//布局方式
GridBagLayout gbl;
GridBagConstraints gc;
AppletContext co;//接口
//初始化
public void init()
{
//new 對(duì)象
labelname=new JLabel("用 戶 名:");
labelpass=new JLabel("用戶密碼:");
labelsxe=new JLabel("性別:");
labeladdress=new JLabel("地址:");
labelmail=new JLabel("電子郵件:");
labelphone=new JLabel("聯(lián)系電話:");
textname=new JTextField(15); textname.setForeground(Color.red);
textname.setToolTipText("請(qǐng)?jiān)谶@輸入你的用戶名");
textaddress=new JTextField(15);textaddress.setForeground(Color.red);
textaddress.setToolTipText("請(qǐng)?jiān)谶@輸入你的地址");
textmail=new JTextField(15); textmail.setForeground(Color.red);
textmail.setToolTipText("請(qǐng)?jiān)谶@里輸入你的E-mail地址");
textphone=new JTextField(15); textphone.setForeground(Color.red);
textphone.setToolTipText("請(qǐng)?jiān)谶@輸入你的電話號(hào)碼");
r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//設(shè)置顏色
r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//設(shè)置顏色
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);//加入組,實(shí)現(xiàn)單選
textpass=new JPasswordField(15);
textpass.setToolTipText("在這里輸入密碼");
textpass.setForeground(Color.red);
buttonregit=new JButton("注冊(cè)"); buttonregit.setBackground(new Color(47,177,210));//設(shè)置顏色
buttonregit.setToolTipText("點(diǎn)擊按紐完成注冊(cè)");
buttonreset=new JButton("填寫(xiě)"); buttonreset.setBackground(new Color(47,177,210));//設(shè)置顏色
buttonreset.setToolTipText("點(diǎn)擊按紐刷新重寫(xiě)");
gbl=new GridBagLayout(); ///////////////////////////////////////
gc=new GridBagConstraints(); //////采用GridBagLayout布局方式////////
p=new JPanel();
p.setLayout(gbl);
p.setBackground(new Color(47,177,210));
this.getContentPane().add(p);//加入面板
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=2;
gbl.setConstraints(labelname,gc);
p.add(labelname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=2;
gbl.setConstraints(textname,gc);
p.add(textname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=4;
gbl.setConstraints(labelpass,gc);
p.add(labelpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=4;
gbl.setConstraints(textpass,gc);
p.add(textpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=6;
gbl.setConstraints(labelsxe,gc);
p.add(labelsxe);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=6;
gbl.setConstraints(r1,gc);
p.add(r1);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=5;
gc.gridy=6;
gbl.setConstraints(r2,gc);
p.add(r2);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=8;
gbl.setConstraints(labeladdress,gc);
p.add(labeladdress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=8;
gbl.setConstraints(textaddress,gc);
p.add(textaddress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=10;
gbl.setConstraints(labelmail,gc);
p.add(labelmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=10;
gbl.setConstraints(textmail,gc);
p.add(textmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=12;
gbl.setConstraints(labelphone,gc);
p.add(labelphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=12;
gbl.setConstraints(textphone,gc);
p.add(textphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=16;
gbl.setConstraints(buttonregit,gc);
p.add(buttonregit);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=16;
gbl.setConstraints(buttonreset,gc);
p.add(buttonreset);
/////////////////////////////////////////////
co=this.getAppletContext();
/////////////////////////////////////////////
buttonregit.addActionListener(this);//按紐事件的監(jiān)聽(tīng)
buttonreset.addActionListener(this);//按紐事件的監(jiān)聽(tīng)
r1.addItemListener(this);//選擇事件的監(jiān)聽(tīng)
r2.addItemListener(this);//選擇事件的監(jiān)聽(tīng)
textphone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//注冊(cè)按紐事件
if(e.getSource()==buttonregit)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字符串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊(cè)資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請(qǐng)重新注冊(cè)!","【溫馨提示】",error);
return;//彈出對(duì)話框并返回
}
try//寫(xiě)入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經(jīng)存在,請(qǐng)更換名字!");
textname.setText("");//設(shè)置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創(chuàng)建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//寫(xiě)
out1.newLine();
out2.write("用戶名:"+s1);
out2.newLine();
out2.write("密碼:"+s2);
out2.newLine();
out2.write("性別:"+sex);
out2.newLine();
out2.write("地址:"+s3);
out2.newLine();
out2.write("電子郵件:"+s4);
out2.newLine();
out2.write("電話:"+s5);
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊(cè)成功!");
try
{
String qss="";
url=new url(/qss);//連接上網(wǎng)址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
//////////////////////以下為回車(chē)事件
if(e.getSource()==textphone)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字符串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊(cè)資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請(qǐng)重新注冊(cè)!","【溫馨提示】",error);
return;//彈出對(duì)話框并返回
}
try//寫(xiě)入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經(jīng)存在,請(qǐng)更換名字!");
textname.setText("");//設(shè)置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創(chuàng)建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//寫(xiě)
out1.newLine();
out2.write("用戶名:"+s1+"密碼:"+s2+"性別:"+sex+"地址:"+s3+"電子郵件:"+s4+"電話:"+s5);//寫(xiě)
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊(cè)成功!");
try
{
String qss="";
url=new url(/qss);//連接上網(wǎng)址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
if(e.getSource()==buttonreset)//刷新重寫(xiě)事件
{
textname.setText("");
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
}
}
//////////////////////////////////////////
//
public void itemStateChanged(ItemEvent ex)
{
if(ex.getSource()==r1)
{
sex=new String("男");
}
else if(ex.getSource()==r2)
{
sex=new String("女");
}
}
}
參考資料:試試看,剛在網(wǎng)上找的