黑马程序员---网络编程(TCP传输 二)

时间:2023-02-21 15:30:27

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

客户端并发访问服务器
为了让多个客户端并发访问服务器,服务器端需要将每个客户端封装在一个单独线程中,这样服务器端就可以处理多个客户端的请求
那么该怎么定义线程呢?
只要明确了每一个客户端要在服务器端执行的代码,将此段代码放在run方法中

服务器端

package fourth;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class PicServer {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(10009);
while(true){
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class PicThread implements Runnable{
// ServerSocket ss;
Socket s;
public PicThread(Socket s) {
this.s=s;
}

@Override
public void run() {
try {
int count=1;
String ip=s.getInetAddress().getHostAddress();
InputStream is=s.getInputStream();
File file=new File("D:\\"+ip+"bmp");
while(file.exists()){
file=new File("D:\\"+ip+"("+(count++)+")"+"bmp");
}

FileOutputStream fos=new FileOutputStream(file);
byte[]b=new byte[1024];
int len=0;
while((len=is.read(b))!=-1){
fos.write(b, 0, len);
}
OutputStream out=s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

客户端

package fourth;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class PicClient {
public static void main(String[] args) {
try {
File file=new File("E:\\123.jpg");

Socket s=new Socket(InetAddress.getLocalHost(), 10009);
FileInputStream fis=new FileInputStream(file);
OutputStream out=s.getOutputStream();
byte[]b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1){
out.write(b, 0, len);
}
s.shutdownOutput();
InputStream in =s.getInputStream();
byte[]bIn=new byte[1024];
int num=in.read(bIn);
System.out.println(new String(bIn,0,num));
fis.close();
s.close();



} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

下面有这样一个需求
客户端通过键盘录入用户名服务器端对这个用户名进行校验
如果用户存在,在服务器端显示xxx已经登录
如果用户不存在,在服务器端显示xxx尝试登录,并在客户端显示用户名不存在,

最多登录三次
服务器端

package fourth;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class LoginServer {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(10010);
while(true){
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}

} catch (IOException e) {
e.printStackTrace();
}
}
}
class UserThread implements Runnable{
Socket s;
public UserThread(Socket s) {
this.s=s;
}
@Override
public void run() {
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connecting...");
try {
for(int i=0 ;i<3;i++){
BufferedReader brIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String name=brIn.readLine();
BufferedReader br=new BufferedReader(new FileReader("E:\\user.txt"));
PrintWriter out=new PrintWriter(s.getOutputStream());
String line=null;
boolean flag=false;
while (null!=(line=br.readLine())) {
if (line.equals(name)) {
flag=true;
break;
}
}
if (flag) {
System.out.println(name+"已登录");
out.println(name+"欢迎光临");
break;
}else{
System.out.println(name+"尝试登录");
out.println(name+"不存在");
}
s.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

定义客户端

package fourth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class LoginClient {
public static void main(String[] args) {
try {
Socket s=new Socket(InetAddress.getLocalHost(), 10012);
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader brIn =new BufferedReader(
new InputStreamReader(s.getInputStream()));
for(int i=0; i<3;i++){
String line=br.readLine();
if(line==null)
break;
out.write(line);

String info=brIn.readLine();
System.out.println("info"+info);
if (info.contains("欢迎"))
break;
}
br.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

既然我们能通过自己定义的客户端访问自己定义的服务器的,当然也可以通过浏览器访问自己定义的服务器端,像下面的例子,自己定义一个服务器端,开启之后,就可以通过浏览器输入地址访问

下面就写个简单的服务器端来演示

package fourth;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(11000);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
System.out.println(ip);
out.println("Hello World");
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

运行这段代码,然后打开览器浏,输入http://xxx.xxx.xxx.xxx:11000/,你本机的IP地址加端口号,就可以访问你的服务器端