import java.io.*;
import java.net.*;
public class LoginClient {
public static void main(String[] args) throws Exception
{
Socket s=new Socket("169.254.91.182",10012);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<3;x++)
{
String line=br.readLine();
if(line==null)
break;
pw.write(line);
String sr= br1.readLine();
if(sr.contains("欢迎"))
break;
System.out.println(sr);
}
br.close();
s.close();
}
}
——————————————————————————————————————————————————————————————————————————
import java.io.*;
import java.net.*;
public class LoginServer {
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10012);
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
class UserThread implements Runnable
{ private Socket s;
UserThread(Socket s)
{
this.s=s;
}
public void run()
{ String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"......connected");
try{
for(int x=0;x<3;x++){
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=br.readLine();
BufferedReader br1=new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\cop_11.txt"));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String l=null;
boolean flag=false;
while((l=br1.readLine())!=null)
{
System.out.println(l);
if(l.equals(name))
{
flag=true;
break;
}
}
if(flag=true)
{
System.out.println(name+":已登录!");
pw.write(name+":欢迎!登入成功!");
break;
}
else
{
System.out.println(name+":正在尝试登陆!");
pw.write("用户名不存在,请重新输入!");
}
s.close();
}}
catch(Exception e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
是按照毕老师的视频来的,可就是运行不了 ,不知道怎么搞的啊?求解释啊 谢谢各位大虾啊
|