第二十四天
综合练习:
客户端通过键盘录入用户名,服务端对用户名进行校验 如果用户名存在在服务端显示已登录并在客户端
显示xxx欢迎光临 如果用户不存在在服务端显示xxx尝试登陆 在客户端显示xxx该用户不存在最多登陆三次
import java.net.*;
import java.io.*;
class LoginClient
{
public static void main(String[]args)throws Exception
{
Socket s = new Socket("192.168.1.100",10000);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x = 0;x<3;x++)
{
String line = bufr.readLine();
if(line==null)
break;
out.println(line);
String info = bufin.readLine();
System.out.println(info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}
class LoginServer
{
public static void main(String[]args)throws Exception
{
ServerSocket ss = new ServerSocket(10000);
while(true)
{
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()
{
try
{
for (int x = 0;x<3 ;x++ )
{
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip);
BufferedReader bufr = new BufferedReader(new FileReader("urse.txt"));
BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String name = bufin.readLine();
if(name==null)
break;
boolean flag = false;
String line = null;
while ((line=bufr.readLine())!=null)
{
if(line.equals(name))
{
flag = true;
break;
}
}
if(flag)
{
System.out.println(name+",已登录");
out.println(name+",欢迎光临");
}
else
{
System.out.println(name+",尝试登陆");
out.println(name+",用户不存在");
}
}
}
catch (Exception e)
{
throw new RuntimeException("校验失败");
}
}
}
URL:
import java.net.*;
class URLDemo
{
public static void main(String[]args)throw MalformedURLException
{
URL url = new URL("http://219.141.157.4:8080/myweb/demo.html?name=haha&age=30");
url.getProtocol();协议名
url.getHost();主机名
url.getPort();端口号
url.getPath();路径
url.getFile();文件名
url.getQuery();参数信息name=haha&age=30
int port = getPort();
if(port==-1)判断端口是否指定否则自己指定默认
port = 80;
}
}
class URLConnectionDemo
{
URL url = new URL("http://219.141.157.4:8080/myweb/demo.html");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[]buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
图形化与客户端结合实例:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Demo
{
private Frame f;
private Button b;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
private Button pkBut;
Demo()
{
init();
}
public void init()
{
f = new Frame("窗口");
f.setBounds(300,200,450,350);
f.setLayout(new FlowLayout());
b = new Button("转到");
tf = new TextField(40);
ta = new TextArea(10,50);
d = new Dialog(f,"提示信息-self",true);
d.setBounds(400,200,240,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(b);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
try
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
catch (Exception ex)
{
}
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
showDir();
}
catch (Exception ex)
{
}
}
});
}
private void showDir()throws Exception
{
ta.setText("");
String urll = tf.getText();
URL url = new URL(urll);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[]buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf,0,len));
}
public static void main(String[]args)
{
new Demo();
}
}
SocketAddress是地址和端口封装在一起
ServerSocket可以指定最大连接数