首先确定两台电脑都在同一个局域网。两台电脑都要装有java的运行环境,(直接两台电脑都装jdk吧 ,jre也是可以的)把代码分别拷贝到两台电脑上,假如用你的电脑做服务器,另一台电脑做客户端,先起你电脑的服务器,再在你电脑起一个客户端,然后在另一台电脑上再起一个客户端,这样两台电脑的客户端就可以通讯了。
创新互联专业为企业提供乐安网站建设、乐安做网站、乐安网站设计、乐安网站制作等企业网站建设、网页设计与制作、乐安企业网站模板建站服务,十多年乐安做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
注意:另一台电脑代码上面的IP地址要正确真上你电脑的IP地址
不懂的再继续问
import java.io.*;
import java.util.*;
import java.net.InetAddress;
public class Ip{
static public HashMap ping; //ping 后的结果集
public HashMap getPing(){ //用来得到ping后的结果集
return ping;
}
//当前线程的数量, 防止过多线程摧毁电脑
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30个线程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
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++){ //对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
//等着所有Ping结束
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);
//读取结果行
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");
//线程结束
threadCount -= 1;
}catch (IOException e){}
}
}
}
在知道里发代码,发截图就好,没必要全贴上来。
局域网聊天的程序原理:
1、在同一个网络。
2、有临时或者永久数据库,进行数据传输与访问。
3、有单独服务器或者用户服务器。
至于具体的学习,我给你个建议吧。
一步一步,照着代码写,你边写,你就会报错,通过解决错误,你就会很快学会了。
祝楼主学习愉快~
//以下引入包
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;
//对象的声明
JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//标签
JTextField textname,textaddress,textmail,textphone;//文本框
JRadioButton r1,r2;//单选按纽
ButtonGroup bg;//组
JPasswordField textpass;//密码域
JButton buttonregit,buttonreset;//注册按纽 重写按纽
JPanel p;//面板
String sex;//定义性别字符串
URL url;//统一资源定位
BufferedWriter out1,out2;//流
BufferedReader in;
//布局方式
GridBagLayout gbl;
GridBagConstraints gc;
AppletContext co;//接口
//初始化
public void init()
{
//new 对象
labelname=new JLabel("用 户 名:");
labelpass=new JLabel("用户密码:");
labelsxe=new JLabel("性别:");
labeladdress=new JLabel("地址:");
labelmail=new JLabel("电子邮件:");
labelphone=new JLabel("联系电话:");
textname=new JTextField(15); textname.setForeground(Color.red);
textname.setToolTipText("请在这输入你的用户名");
textaddress=new JTextField(15);textaddress.setForeground(Color.red);
textaddress.setToolTipText("请在这输入你的地址");
textmail=new JTextField(15); textmail.setForeground(Color.red);
textmail.setToolTipText("请在这里输入你的E-mail地址");
textphone=new JTextField(15); textphone.setForeground(Color.red);
textphone.setToolTipText("请在这输入你的电话号码");
r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//设置颜色
r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//设置颜色
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);//加入组,实现单选
textpass=new JPasswordField(15);
textpass.setToolTipText("在这里输入密码");
textpass.setForeground(Color.red);
buttonregit=new JButton("注册"); buttonregit.setBackground(new Color(47,177,210));//设置颜色
buttonregit.setToolTipText("点击按纽完成注册");
buttonreset=new JButton("填写"); buttonreset.setBackground(new Color(47,177,210));//设置颜色
buttonreset.setToolTipText("点击按纽刷新重写");
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);//按纽事件的监听
buttonreset.addActionListener(this);//按纽事件的监听
r1.addItemListener(this);//选择事件的监听
r2.addItemListener(this);//选择事件的监听
textphone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//注册按纽事件
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(); ////////////////////
//判断注册资料 信息 是否为空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);
return;//弹出对话框并返回
}
try//写入到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,"用户名已经存在,请更换名字!");
textname.setText("");//设置为空,重新输入
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));
}//创建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//写
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,"注册成功!");
try
{
String qss="";
url=new url(/qss);//连接上网址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
//////////////////////以下为回车事件
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(); ////////////////////
//判断注册资料 信息 是否为空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);
return;//弹出对话框并返回
}
try//写入到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,"用户名已经存在,请更换名字!");
textname.setText("");//设置为空,重新输入
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));
}//创建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//写
out1.newLine();
out2.write("用户名:"+s1+"密码:"+s2+"性别:"+sex+"地址:"+s3+"电子邮件:"+s4+"电话:"+s5);//写
out2.newLine();
out1.flush();
out2.flush();//清理缓冲
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注册成功!");
try
{
String qss="";
url=new url(/qss);//连接上网址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
if(e.getSource()==buttonreset)//刷新重写事件
{
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("女");
}
}
}
参考资料:试试看,刚在网上找的