62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.net.*;
public class tcpjieshou
{
static ServerSocket ss;
Socket socket;
OutputStream os;
public tcpjieshou(){
try{
ss=new ServerSocket(13333);
System.out.println( "waiting for client to connect on... ");
}catch(Exception e){System.out.println("端口问题");}
}
void tcpjieshoutest() throws SocketException, IOException
{
os=new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\tcpjieshou00.rmvb"));
System.out.println("start....");
InputStream socketis = null;
byte []buff=new byte[60000];
try{
while(true)
{
socket=ss.accept();
System.out.println(socket);
socketis=new BufferedInputStream(socket.getInputStream());
socketis.read(buff);
os.write(buff);
os.flush
System.out.println("jieshou");
}
}catch(Exception e){
System.out.println("while:"+e); }
}
public static void main(String[] args) throws SocketException, IOException {
new tcpjieshou().tcpjieshoutest();
}
}
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
static ServerSocket ss;
Socket socket;
public Test() {
try {
ss = new ServerSocket(13333); //监听端口
System.out
.println("waiting for client to connect on... ");
} catch (Exception e) {
System.out.println("端口问题");
}
}
void tcpjieshoutest() throws Exception {
System.out.println("start....");
try {
while (true) {
socket = ss.accept(); //再一次回到这时会停止。不会向下执行
Thread t = new MyThread(socket);
t.start();
}
} catch (Exception e) {
System.out.println("while:" + e);
}
}
public static void main(String[] args) throws Exception {
new Test().tcpjieshoutest();
}
}
class MyThread extends Thread{
private Socket socket;
public MyThread(Socket s){
this.socket = s;
}
public void run(){
try{
OutputStream os = new BufferedOutputStream(new FileOutputStream(
"1.txt",true));
System.out.println(socket);
BufferedReader socketis = null;
socketis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String buff = null;
while((buff=socketis.readLine())!=null){ //因为这里只接受一行数据
os.write(buff.getBytes());
os.flush();
System.out.println("jieshou");
}
}
catch(Exception e){}
}
}