网络编程实例
1、从客户端传到服务端,从键盘上输入
package com.hbsr.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TcpClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Socket s=new Socket("192.168.49.32",9009);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//输出流,只写不读,从键盘上输入,用outputStream进行写
OutputStream out=s.getOutputStream();
BufferedWriter bwout=new BufferedWriter(new OutputStreamWriter(out));
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream
()));
String line=null;
while((line=br.readLine())!=null){
if(line.equals("over"))
break;
bwout.write(line);
bwout.newLine();
bwout.flush();
String str=brin.readLine();
System.out.println("sever:"+str);
}
br.close();
s.close();
}
}
package com.hbsr.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//1、建立服务器socket
ServerSocket ss=new ServerSocket(9009);
//2、调用accept()
Socket s=ss.accept();
//3、获取哪个ip和服务器进行连接
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"……connection");
//输入流,读取客户端的内容
InputStream in=s.getInputStream();
BufferedReader brin=new BufferedReader(new InputStreamReader(in));
BufferedWriter brout=new BufferedWriter(new OutputStreamWriter
(s.getOutputStream()));
String line=null;
while((line=brin.readLine())!=null){
System.out.println("client:"+line);
brout.write(line.toUpperCase());
brout.newLine();
brout.flush();
}
s.close();
ss.close();
}
}
2、上传文件
package com.hbsr.net;
import java.net.*;
import java.io.*;
public class UplodClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket s=new Socket("192.168.49.14",9009);
BufferedReader br=new BufferedReader(new FileReader("F:\\文件\\记事本\\密码.txt"));
PrintWriter pwout=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=br.readLine())!=null){
pwout.println(line);
}
s.shutdownOutput();
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=brin.readLine();
System.out.println(str);
br.close();
s.close();
}
}
package com.hbsr.net;
import java.net.*;
import java.io.*;
public class UplodSever {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9009);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"....connection");
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new FileWriter("E:\\server.txt"),true);
String line=null;
while((line=brin.readLine())!=null){
pw.println(line);
}
PrintWriter pwout=new PrintWriter(s.getOutputStream(),true);
pwout.println("上传成功!");
pw.close();
s.close();
ss.close();
}
}
3、上传图片(利用线程)
package com.hbsi.net;
import java.net.*;
import java.io.*;
public class JpgClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
if(args.length==0){
System.out.println("请指定一个jpg文件");
return;
}
File f=new File(args[0]);
Socket s=new Socket("192.168.49.14",9009);
FileInputStream fs=new FileInputStream(f);
OutputStream out=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fs.read(buf))!=-1){
out.write(buf, 0, len);
}
s.shutdownOutput();
InputStream in=s.getInputStream();
byte[] b=new byte[1024];
int num=in.read(b);
System.out.println(new String(b,0,num));
fs.close();
s.close();
}
}
package com.hbsi.net;
import java.net.*;
import java.io.*;
public class JpgServer {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9009);
while(true){
Socket s=ss.accept();
new Thread(new JpgThread(s)).start();
}
}
}
class JpgThread implements Runnable{
private Socket s;
public JpgThread(Socket s){
this.s=s;
}
public void run() {
int count=1;
try {
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"....connection");
InputStream in=s.getInputStream();
File dir=new File("E:\\picture");
File f=new File(dir,ip+"("+(count++)+").jpg");
while(f.exists()){
f=new File(dir,ip+"("+(count++)+").jpg");
}
FileOutputStream fos=new FileOutputStream(f);
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) {
e.printStackTrace();
}
}
}