Tcp客服端并发访问服务器
2014-03-03 21:48
411 查看
/*线程*/
import java.net.*;
import java.io.*;
public class PicThread implements Runnable {
private Socket s;
public ClientThread(Socket ss) {
this.s = ss;
}
@Override
public void run() {
int count=1;
try{
String ip = s.getInetAddress().getHostAddress();
File file = new File("D:\\"+ip+"("+count+")"+".jpg");
while(file.exists()){
file = new File("D:\\"+ip+"("+(count++) +")"+".jpg");
}
InputStream in = s.getInputStream();
BufferedOutputStream buffin=
new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!=-1){
buffin.write(buf, 0, len);
buffin.flush();
}
OutputStream out =s.getOutputStream();
byte[] info = "上传成功".getBytes();
out.write(info);
}catch(Exception e){
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*客服端*/
import java.io.*;
import java.net.*;
public class PicClient {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//创建服务端点
Socket s = new Socket("127.0.0.1",10000);
// 读文件
BufferedInputStream buffin = new BufferedInputStream( new FileInputStream("D:\\3.jpg"));
OutputStream out = s.getOutputStream();
byte[] buf= new byte[1024];
int len =0;
// 往服务端写文件
while((len = buffin.read(buf))!=-1){
out.write(buf,0,len);
out.flush();
}
s.shutdownOutput();//告诉服务端 文件写完啦
// 接收服务端的反馈信息
InputStream in = s.getInputStream();
byte [] bfin = new byte[1024];
int lenIn = in.read(bfin);
System.out.println(new String(bfin,0,lenIn));
buffin.close();
}
}
/*服务器*/
import java.net.*;
import java.io.*;
public class PicServer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10000);
while(true){
Socket socket=ss.accept();
new Thread(new ClientThread(socket)).start();
}
import java.net.*;
import java.io.*;
public class PicThread implements Runnable {
private Socket s;
public ClientThread(Socket ss) {
this.s = ss;
}
@Override
public void run() {
int count=1;
try{
String ip = s.getInetAddress().getHostAddress();
File file = new File("D:\\"+ip+"("+count+")"+".jpg");
while(file.exists()){
file = new File("D:\\"+ip+"("+(count++) +")"+".jpg");
}
InputStream in = s.getInputStream();
BufferedOutputStream buffin=
new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!=-1){
buffin.write(buf, 0, len);
buffin.flush();
}
OutputStream out =s.getOutputStream();
byte[] info = "上传成功".getBytes();
out.write(info);
}catch(Exception e){
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*客服端*/
import java.io.*;
import java.net.*;
public class PicClient {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//创建服务端点
Socket s = new Socket("127.0.0.1",10000);
// 读文件
BufferedInputStream buffin = new BufferedInputStream( new FileInputStream("D:\\3.jpg"));
OutputStream out = s.getOutputStream();
byte[] buf= new byte[1024];
int len =0;
// 往服务端写文件
while((len = buffin.read(buf))!=-1){
out.write(buf,0,len);
out.flush();
}
s.shutdownOutput();//告诉服务端 文件写完啦
// 接收服务端的反馈信息
InputStream in = s.getInputStream();
byte [] bfin = new byte[1024];
int lenIn = in.read(bfin);
System.out.println(new String(bfin,0,lenIn));
buffin.close();
}
}
/*服务器*/
import java.net.*;
import java.io.*;
public class PicServer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10000);
while(true){
Socket socket=ss.accept();
new Thread(new ClientThread(socket)).start();
}
相关文章推荐
- UNIX网络编程读书笔记:图解TCP端口号和并发服务器
- 高并发访问时如何确保服务器端session过多而造成内存溢出致使服务器宕机的方法之一
- 简单建立基于Linux建立并发TCP服务器
- 实现TCP并发服务器之四(poll函数)
- tcp多线程并发服务器
- 在DragonBoard 410c上实现高并发处理TCP服务器
- Linux下tcp并发服务器的几种设计的模式套路
- Linux select TCP并发服务器与客户端编程
- 高性能、高并发TCP服务器(多线程调用libevent)
- C#SocketAsyncEventArgs实现高效能多并发TCPSocket通信 (服务器实现)
- TCP并发服务器模型(一)
- 高性能、高并发TCP服务器(多线程调用libevent)
- 高性能、高并发TCP服务器(多线程调用libevent)
- 查看服务器tcp连接及服务器并发
- 实现TCP并发服务器之五(epoll函数)
- socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数可以有多少
- TCP并发服务器(七)——可动态增减的线程池,主线程accept——基于UNP代码修改
- 第5章 TCP服务器-客服端程序例子
- Linux下tcp并发服务器的几种设计的模式套路
- TCP多线程并发服务器+线程池+echo