本帖最后由 一碗小米周 于 2013-12-5 13:02 编辑
- package gui;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class LoginInClient {
- public static void main(String[] args){
- try
- {
- Socket s = new Socket("172.16.21.50",10030);
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter pw =new PrintWriter(s.getOutputStream(),true);
- BufferedReader brr =new BufferedReader(new InputStreamReader(s.getInputStream()));
- for(int i=0;i<3;i++){
- String line =br.readLine();
- pw.println(line);
- s.shutdownOutput();
- String str =brr.readLine();
- System.out.println(str);
- }
-
- br.close();
- s.close();
-
-
- }
- catch (UnknownHostException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
复制代码
- package gui;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class LoginInServer {
- public static void main(String[] args){
- try
- {
- ServerSocket ss = new ServerSocket(10030);
- Socket s = ss.accept();
- System.out.println("shhit");
- while(true)
- {
- new Thread(new myThread2(s)).start();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
- class myThread2 implements Runnable{
- private Socket s;
- public myThread2(Socket s) {
- this.s=s;
- }
- public void run(){
- try
- {
- for(int i=0;i<3;i++){
- BufferedReader br =new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = br.readLine();
- BufferedReader brr = new BufferedReader(new FileReader("E:\\sql.txt"));
-
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- String line =null;
- while((line=brr.readLine())!=null){
- if(str.equals(line))
- {
- pw.println("登陆成功");
- break;
- }
- else {
- pw.println("登陆失败,请尝试登陆!");
- break;
- }
- }
-
- }
- }
- catch (Exception e)
- {
-
- }
- }
- }
-
-
复制代码
上面是我写的一个登陆到服务器的一个简单小程序。但是当我开启服务器段的时候,再开启客户端,myeclipse就会死了。cpu运行达到100%。出现卡机现象。求大神帮忙看看啊。谢谢各位了。
|