练习:复制文本
练习要求:把客户端的一个文件内容发到服务端,在服务端把数据存储到一个文件当中。相当于复制文件。
import java.io.*;
import java.net.*;
class TxtCopyTestClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.6",10003);
BufferedReader bur=new BufferedReader(new InputStreamReader(new FileInputStream("client.txt")));
//BufferedWriter buwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=bur.readLine())!=null)
{
pw.println(line);
}
s.shutdownOutput();
BufferedReader burIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=burIn.readLine();
System.out.println(str);
bur.close();
s.close();
}
}
class TxtCopyTestServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10003);
Socket s=ss.accept();
BufferedReader burIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new FileOutputStream("server.txt"),true);
String line=null;
while((line=burIn.readLine())!=null)
{
pw.println(line);
}
pw.close();
PrintWriter pwOut=new PrintWriter(s.getOutputStream(),true);
pwOut.println("上传成功");
s.close();
ss.close();
}
}
练习2:上传图片
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.114",10003);
FileInputStream fis=new FileInputStream("pictest.jpg");
OutputStream os=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
os.write(buf,0,len);
}
s.shutdownOutput();
InputStream is=s.getInputStream();
byte[] re=new byte[1024];
int length=is.read(re);
String str=new String(re,0,length);
System.out.println(str);
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10003);
Socket s=ss.accept();
InputStream is=s.getInputStream();
FileOutputStream fos=new FileOutputStream("serverpic.jpg");
byte[] buf=new byte[1024];
int len=0;
while((len=is.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream os=s.getOutputStream();
os.write("上传成功".getBytes());
s.close();
ss.close();
}
}
练习3:客户端并发上传图片
import java.io.*;
import java.net.*;
class UploadByThreadClient
{
public static void main(String[] args) throws Exception
{
if(args.length!=1)
{
System.out.println("请选择一张jpg图片");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
System.out.println("该文件有问题,要么补存在,要么不是文件");
return ;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
return ;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件过大,没安好心");
return ;
}
Socket s = new Socket("192.168.1.114",10003);
FileInputStream fis = new FileInputStream("pictest.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
//告诉服务端数据已写完
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
class UploadByThreadServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10003);
while(true)
{
Socket s=ss.accept();
new Thread(new AcceptThread(s)).start();
}
}
}
class AcceptThread implements Runnable
{
private Socket s;
AcceptThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
InputStream in = s.getInputStream();
String ip=s.getInetAddress().getHostAddress();
int count=1;
File def=new File(ip+"("+count+")"+".jpg");
while(def.exists())
{
def=new File(ip+"("+(count++)+")"+".jpg");
}
FileOutputStream fos = new FileOutputStream(def);
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException("上传失败");
}
}
}
练习4:客户端并发登陆
import java.io.*;
import java.net.*;
class LoginClient
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.1.6",100010);
BufferedReader bur=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader burIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int i=0;i<3;i++)
{
String user=bur.readLine();
if(user==null)
break;
pw.println(user);
String info=burIn.readLine();
System.out.println("反馈信息为:"+info);
if(info.contains("欢迎"))
{
System.out.println("登陆成功");
break;
}
}
bur.close();
s.close();
}
}
class LoginServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(100010);
while(true)
{
Socket s=ss.accept();
System.out.println("连接成功");
new Thread(new UserLoginThread(s)).start();
}
}
}
class UserLoginThread implements Runnable
{
private Socket s;
UserLoginThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
BufferedReader burIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int i=0;i<3;i++)
{
String user=burIn.readLine();
System.out.println("发送来的用户名:"+user);
if(user==null)
{
System.out.println("为空"+user);
break;
}
BufferedReader bur=new BufferedReader(new FileReader("user.txt"));
String line=null;
boolean flag=false;
while((line=bur.readLine())!=null)
{
System.out.println("搜索到的用户名:"+line);
if(line.equals(user))
{
flag=true;
break;
}
}
if(flag)
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(user+"欢迎您!");
s.close();
break;
}
else
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("请输入正确的用户名");
}
bur.close();
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException("登陆失败");
}
}
}
时间: 2024-10-06 07:46:34