socket 编程

来源:互联网 发布:收银系统怎么删除数据 编辑:程序博客网 时间:2024/06/04 19:38

这次是把上一次登录系统改成了C/S模式的小程序。去掉了管理员系统,没其他原因,就是尼玛太累了,最近真的快忙死了,但是感觉压力又好大,大三马上就要大四了,一想到毕业我就烦。而且老师讲的我很跟不上,就像Java,一星期上一下午的课,一次要上好几章,我得用一个星期的时间去把老师讲的编一遍,自己再找点小项目练习一遍,唉,力不从心啊!!!

废话不说了,把代码帖一下:

以下是服务器部分:

import java.awt.*;import java.awt.event.*;import java.net.*;import java.util.*;import java.io.*;import javax.swing.*;public class socket//主类部分是用来跟客户端进行信息的交互{public static int port=4444;public static void main(String args[]) throws IOException{String []br=new String[2];ServerSocket s=new ServerSocket(port);System.out.println("服务器开始工作"+s);Socket so=s.accept();try{System.out.println("客户端已经链接到服务器上"+so);BufferedReader in=new BufferedReader(new InputStreamReader(so.getInputStream()));PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(so.getOutputStream())),true);while(true){String str=in.readLine();//这里把客户端发来的字符串进行分解String str1=str.substring(0, 2);//分成前两个和后面所有int a=str.length();String str2=str.substring(2, a);if(str1.equals("Na"))//如果钱两个字符是NA,则注册名字{writename w=new writename(str2);}if(str1.equals("Pa"))//再注册密码{writepass p=new writepass(str2);String reg="regsuccess";out.println(reg);//向客户端返回注册成功的信息break;}if(str1.equals("LN"))//如果是LN,则查找名字{br[0]=str2;}if(str1.equals("LP"))//和密码{    br[1]=str2;    read r=new read(br);//如果存在,则返回登录成功    out.println(read.str);    break;}}}finally{System.out.println("关闭链接");so.close();s.close();}}}class writename//注册名字的部分{String name;writename(String na){this.name=na;try{File f1=new File("name.txt");RandomAccessFile rwf1=new RandomAccessFile(f1,"rw");long len1=rwf1.length();rwf1.seek(len1);rwf1.writeBytes(""+'\r'+'\n');rwf1.writeBytes(name);rwf1.close();}catch(Exception e){}}}class writepass//注册密码的部分{String pass;writepass(String pa){this.pass=pa;try{File f2=new File("password.txt");RandomAccessFile rwf2=new RandomAccessFile(f2,"rw");long len2=rwf2.length();rwf2.seek(len2);rwf2.writeBytes(""+'\r'+'\n');rwf2.writeBytes(pass);rwf2.close();}catch(Exception e){}}}class read//查找部分{String name,pass;static String str="loadfalse";long count1,count2;read(String a[]){name=a[0];pass=a[1];try{File f1=new File("name.txt");File f2=new File("password.txt");RandomAccessFile rwf1=new RandomAccessFile(f1,"rw");RandomAccessFile rwf2=new RandomAccessFile(f2,"rw");count1=0;count2=0;long len1;len1=rwf1.length();while(count1<len1){String str1=rwf1.readLine();String str2=rwf2.readLine();count1=rwf1.getFilePointer();count2=rwf2.getFilePointer();if((str1.equals(name))&&(str2.equals(pass))){str="loadsuccess";break;}}rwf1.close();rwf2.close();}catch(Exception e){}}}


 

再以下是客户端:

import java.awt.*;import java.awt.event.*;import java.net.*;import java.util.*;import java.io.*;import javax.swing.*;public class linad{public static void main(String args[]){lin l=new lin();}}class lin extends JFrame implements ActionListener {JTextField t1,t2;JLabel l1,l2;JButton b,b1;JPanel p;cas c;lin(){JFrame f=new JFrame();f.setTitle("服务器和客户端");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);Container con=f.getContentPane();f.setSize(350,300);p=new JPanel();p.setLayout(null);t1=new JTextField(20);t2=new JTextField(20);b=new JButton("注册");b1=new JButton("登录");l1=new JLabel("名字:");l2=new JLabel("密码:");p.add(t1);p.add(t2);p.add(b);p.add(b1);p.add(l1);p.add(l2);con.add(p);l1.setBounds(20, 30, 60, 25);l2.setBounds(20, 60, 60, 25);t1.setBounds(80, 30, 150, 25);t2.setBounds(80, 60, 150, 25);b.setBounds(20, 100, 60, 30);b1.setBounds(100, 100, 60, 30);b.addActionListener(this);b1.addActionListener(this);}public void actionPerformed(ActionEvent e) {if(e.getSource()==b){try{    String str1="Na"+t1.getText();//这里把名字和密码前面加上na和pa,用来服务器端做判断用的    String str2="Pa"+t2.getText();    link l11=new link(str1,str2);    l11.ll();}catch(IOException ie){}}if(e.getSource()==b1){try{String str1="LN"+t1.getText();//和上面一样,如果是NA,PA则进行注册,是LA,LP则进行查找,比对String str2="LP"+t2.getText();link l11=new link(str1,str2);l11.ll();}catch(IOException ie){}if(link.a.equals("loadsuccess"))//当成功过登录后有欢迎的动画{t1.setVisible(false);t2.setVisible(false);b1.setVisible(false);b.setVisible(false);l1.setVisible(false);l2.setVisible(false);c=new cas(false);    c.setBounds(0, 0, 350, 300);    cas.ca=true;    p.add(c);}}}}class link//此部分是最主要的,用来链接服务器,并且和服务器进行互动{String str1,str2;static String a;link(String s1,String s2){this.str1=s1;this.str2=s2;}public void ll() throws IOException{int port=4444;InetAddress ad=InetAddress.getByName("localhost");Socket s=new Socket(ad,port);try{BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);out.println(str1);//依次把名字和密码发给客户端out.println(str2);a=in.readLine();//根据客户端返回的信息进行判断操作if(a.equals("regsuccess")){JOptionPane.showMessageDialog(null, "注册成功!");}if(a.equals("loadfalse")){JOptionPane.showMessageDialog(null, "登录失败,请注册先");}if(a.equals("loadsuccess")){JOptionPane.showMessageDialog(null, "登录成功!");}}finally{System.out.println("链接断开");s.close();}}}class cas extends Canvas implements Runnable//登录成功后的动画效果,其实就是一行字啦{static boolean chco=false,ca=false;Thread trd=new Thread(this);int x=0,y=0;Font fnt=new Font("隶书",Font.BOLD,18);cas(boolean changecolor){chco =changecolor;setBackground(Color.CYAN);trd.start();}public void run(){while(true){x=x+2;y=y+2;if(y>300){x=0;y=0;}repaint();try{trd.sleep(300);}catch(InterruptedException e){}}}public void paint(Graphics g){if(chco){g.setColor(Color.red);}else {g.setColor(Color.CYAN);}g.setFont(fnt);if(ca){g.setColor(Color.red);g.drawString("welcome!!!",  x, y);}}}


      


小结:

1、很乱,因为时间仓促老师也只讲了一节课,而且这部分是书上没有的,老师说很重要才上的,我基本上只能自己上网查资料,很多东西似懂非懂

2、这个程序只能让客户端和服务器连接一次,比如注册或者登录成功后都会自动断开,我试了几种方法没用就不想弄了,唉,没心情。等有心情的时候再修改

3、接第二条,这个程序也只能同时连接一个客户端,貌似可以用线程的方法让很多客户端同时连接上来,等有心情的时候再弄。。

原创粉丝点击