java实验一socket.docx
- 文档编号:8764308
- 上传时间:2023-05-14
- 格式:DOCX
- 页数:18
- 大小:147.74KB
java实验一socket.docx
《java实验一socket.docx》由会员分享,可在线阅读,更多相关《java实验一socket.docx(18页珍藏版)》请在冰点文库上搜索。
java实验一socket
JAVA程序设计
实验目的:
Socket编程
专业班级:
电子商务10-01班
学号:
姓名:
任课教师:
时间:
2012年11月28日
一,实验题目:
通过对Socket编程的学习,尝试编写一个简单的交互程序.
二,实验内容:
我想使用Socket编写一个带有简单界面并能够实现多个客户端和多个服务器端实时交互的程序,步骤如下:
在编写客户端的程序时,我想先建立一个登陆界面,用以身份验证;当成功登陆后,在创建一个服务器列表界面,用以显示可供连接的各个服务器;点击相应的按钮,则创建一个相应的线程且能够实现用以聊天的界面.
交互的实现,我想创建两个线程,分别进行客户端用户发送和接受信息.
在服务器端,因为实验主要实现多对多分别进行交互,我就只创建一个用以服务器用户进行信息输入的界面,该界面也继承Thread类实现与多个客户端的连接;并且,当有与相应的客户端连接时弹出该界面;交互的实现也采用输入输出两个线程分别进行.
三,实验清单:
客户端程序如下:
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
importjava.awt.*;
importjava.io.*;
import.*;
importjava.util.*;
importjava.text.*;
publicclassClientCode
{
publicstaticvoidmain(Stringargs[]){
newDengLu();//创建登陆界面
}
}
classDengLuextendsJFrame{
Stringusername="o";//没有后台数据库,我自己设置一个账号密码
Stringpassword="o";//以后可变更
JButtonbLogin=newJButton("登录");//创建登陆按钮
JButtonbCancel=newJButton("取消");//创建取消按钮
JLabellblUser=newJLabel("账号:
");
JLabellblPassword=newJLabel("密码:
");
JPasswordFieldtxtPassword=newJPasswordField(15);
JTextFieldtxtUser=newJTextField(15);
JPanelp1;
DengLu(){
super("登录界面");
setLayout(null);
p1=newJPanel();
p1.setBounds(0,0,250,140);
p1.add(lblUser);
p1.add(txtUser);
p1.add(lblPassword);
p1.add(txtPassword);
p1.add(bLogin);
p1.add(bCancel);
add(p1);
setBounds(300,300,250,140);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bLogin.addActionListener(newActionListener(){//登陆按钮添加监听器,当
publicvoidactionPerformed(ActionEvente){//触发时,验证账号密码;
Strings1=txtUser.getText();//正确后,创建一个服务器
Strings2=String.valueOf(txtPassword.getPassword());//列表界面,供客户
if(s1.equals(username)&&s2.equals(password)){//选择需要连接的
newLieBiao();//服务器;登陆失败时,弹出提示框
}else{
JOptionPane.showMessageDialog(null,"用户名和密码错误,请重新输");
}
}
});
bCancel.addActionListener(newActionListener(){//取消按钮添加监听器
publicvoidactionPerformed(ActionEvente){//点击时,将输入数据重新清空
txtUser.setText("");
txtPassword.setText("");
}
});
}
}
classLieBiaoextendsJFrame//这里有三个按钮,点击时可以分别连接服务器
{
publicLieBiao(){
super("服务器列表");
Containercontain=getContentPane();
contain.setLayout(newBorderLayout());
JPanelpan=newJPanel();
pan.setLayout(newFlowLayout(FlowLayout.CENTER));
JButtonsen1=newJButton("服务器一");//创建服务器连接按钮一,点击弹出聊天对话框
sen1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newLiaoTian();
}
});
JButtonsen2=newJButton("服务器二");//创建服务器连接按钮一,点击弹出聊天对话框
sen2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newLiaoTian();
}
});
JButtonsen3=newJButton("服务器三");//创建服务器连接按钮一,点击弹出聊天对话框
sen3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newLiaoTian();
}
});
pan.add(sen1);
pan.add(sen2);
pan.add(sen3);
contain.add(pan,BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
classLiaoTianextendsJFrameimplementsRunnable{//聊天对话框,实现与多服务端的通信
JTextAreamainArea;
JTextFieldsendField;
JTextFieldipArea;
Stringd="";
Socketsocket=null;
publicLiaoTian(){
super("聊天");
Threadt=newThread(this);
t.start();
}
publicvoidrun(){
try{
socket=newSocket("127.0.0.1",8000);}catch(Exceptione){}
Containercontain=getContentPane();
contain.setLayout(newBorderLayout());
mainArea=newJTextArea();
JScrollPanemainAreaP=newJScrollPane(mainArea);
JPanelpanel=newJPanel();
panel.setLayout(newBorderLayout());
sendField=newJTextField("客户端说",20);
DateFormatnformat=DateFormat.getTimeInstance();
JButtonsendBtn=newJButton("发送");//创建发送按钮,点击按钮,创建一个发送线
sendBtn.addActionListener(newActionListener(){//程,用来与服务器交互时,发送
publicvoidactionPerformed(ActionEventae){//信息
d=sendField.getText();
DateFormatnformat=DateFormat.getTimeInstance();//获取本机的时间
Stringti=nformat.format(newDate());//主要用于确认发送的时间
mainArea.append("客户端说:
"+d+"\t\t\t"+ti+"\n");//客户端显示自己的
newFaSong(socket,d,ti);//发送的信息
sendField.setText("");
}
});
try{
newJieShou(socket,mainArea);//创建接受信息的线程,专门用于接受相应
}//服务器端发送的信息
catch(Exceptione){
e.printStackTrace();
}
JPanelipPanel=newJPanel();
ipPanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10));
ipPanel.add(newJLabel("我是客户端"));
ipArea=newJTextField(12);
ipArea.setText("服务器"+socket.getRemoteSocketAddress());
ipPanel.add(ipArea);
JButtonbtnLink=newJButton("关闭");
ipPanel.add(btnLink);
btnLink.addActionListener(newActionListener(){//点击按钮时,退出程序
publicvoidactionPerformed(ActionEventae){//这里可改变其功能
System.exit(0);
}
});
panel.add(sendBtn,BorderLayout.EAST);
panel.add(sendField,BorderLayout.CENTER);
contain.add(ipPanel,BorderLayout.NORTH);
contain.add(mainAreaP,BorderLayout.CENTER);
contain.add(panel,BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
classFaSongextendsThread{//用于发送的线程
BufferedReaderen=null;
PrintWriterout=null;
Stringinfo;
Stringti;
publicFaSong(Socketa,Strings,Stringme)
{
try{
info=s;
ti=me;
out=newPrintWriter(newBufferedWriter(
newOutputStreamWriter(a.getOutputStream())),true);
start();
}
catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun()
{
try{
while(true)
{
if(!
info.equalsIgnoreCase(""))
{
out.println(info+"\t\t\t"+ti);
if(info.equals("bye"))System.exit(0);
info="";
}
}
}
catch(Exceptione){
e.printStackTrace();
}
}
}
classJieShouextendsThread//用于接受信息的线程
{
JTextAreare;
BufferedReaderin=null;
PrintWriterout=null;
Stringinfo;
publicJieShou(Socketa,JTextAreama)
{
try{
re=ma;
in=newBufferedReader(newInputStreamReader(a.getInputStream()));
start();
}
catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun()
{
try{
while(true){
info=in.readLine();
re.append("服务端说:
"+info+"\n");
if(info.equals("bye"))System.exit(0);
}
}
catch(Exceptione){
e.printStackTrace();
}
}
}
服务器端程序如下:
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
importjava.awt.*;
importjava.io.*;
import.*;
importjava.util.*;
importjava.text.*;
publicclassServerCodeextendsThread
{
staticServerSockets=null;
staticSocketsocket=null;
publicstaticvoidmain(String[]args){
try{
s=newServerSocket(8000);
while(true){//循环监听客户端请求,
socket=s.accept();//连接上一个客户端就创建
newServerCode().start();//一个交互的服务器线程
}
}
catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun(){
newLiao(socket);//创建聊天界面,并将socket的引用传递给该聊天界面
}
}
classLiaoextendsJFrame{
JTextAreamainArea;
JTextFieldsendField;
JTextFieldipArea;
Stringd="";
Socketsocket=null;
publicLiao(Sockets){
super("聊天");
socket=s;
Containercontain=getContentPane();
contain.setLayout(newBorderLayout());
mainArea=newJTextArea();
JScrollPanemainAreaP=newJScrollPane(mainArea);
JPanelpanel=newJPanel();
panel.setLayout(newBorderLayout());
sendField=newJTextField("服务器说",20);
JButtonsendBtn=newJButton("发送")//创建发送按钮,点击时能
sendBtn.addActionListener(newActionListener(){//将服务器的信息发给客户机
publicvoidactionPerformed(ActionEventae){
d=sendField.getText();
DateFormatnformat=DateFormat.getTimeInstance();
Stringti=nformat.format(newDate());//获取本地机器的时间,并且显示
mainArea.append("服务端说:
"+d+"\t\t\t"+ti+"\n");
newFa(socket,d,ti);//将socket应用在传递给负责发送信息的线程
sendField.setText("");
}
});
try{
newJie(socket,mainArea);
}
catch(Exceptione){
e.printStackTrace();
}
JPanelipPanel=newJPanel();
ipPanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10));
ipPanel.add(newJLabel("我是服务端:
"));
ipArea=newJTextField(12);
ipArea.setText("客户端"+socket.getRemoteSocketAddress())//获取相应客户端的
ipPanel.add(ipArea);//地址及端口号并显示
JButtonbtnLink=newJButton("关闭");
ipPanel.add(btnLink);
btnLink.addActionListener(newActionListener(){//点击按钮时退出程序
publicvoidactionPerformed(ActionEventae){
System.exit(0);
}
});
panel.add(sendBtn,BorderLayout.EAST);
panel.add(sendField,BorderLayout.CENTER);
contain.add(ipPanel,BorderLayout.NORTH);
contain.add(mainAreaP,BorderLayout.CENTER);
contain.add(panel,BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
classFaextendsThread//创建用以发送信息的线程
{
BufferedReaderen=null;
PrintWriterout=null;
Stringinfo;
Stringti;
publicFa(Socketa,Strings,Stringmr)//这里的a已经指向了最初的socket
{
try{
info=s;
ti=mr;
out=newPrintWriter(newBufferedWriter(
newOutputStreamWriter(a.getOutputStream())),true);//获取输出流
start();
}
catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun()
{
try{
while(true){
if(!
info.equalsIgnoreCase("")){
out.println(info+"\t\t\t"+ti);
if(info.equals("bye"))System.exit(0);
info="";
}
}
}
catch(Exceptione){
e.printStackTrace();
}
}
}
classJieextendsThread//创建接受信息的线程,负责接受客户端的信息
{
JTextAreare;
BufferedReaderin=null;
PrintWriterout=null;
Stringinfo;
publicJie(Socketa,JTextAreama){//这里的a也是指向了客户端连接对应的socket
try{
re=ma;
in=newBufferedReader(newInputStreamReader(a.getInputStream()));
start();//获取输入流
}
catch(Exceptione){
e.printStackTrace();
}
}
publicvoidrun()
{
try{
while(true){
info=in.readLine();
re.append("客户端说:
"+info+"\n");//显示输出接收到的信息
if(info.equals("bye"))System.exit(0);//接受到bye字符串就退出程序
}
}
catch(Exceptione){
e.printStackTrace();
}
}
}
实验结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 socket