当前位置: 代码迷 >> J2SE >> java tcp接受数据解决思路
  详细解决方案

java tcp接受数据解决思路

热度:135   发布时间:2016-04-24 02:18:56.0
java tcp接受数据
Java code
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();        }            }    

我运行这个程序结果只显示一次jieshou,说明只循环了一次,这是怎么搞的?

------解决方案--------------------
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class SendMessage extends Thread {
private String ip;
private int i;
Socket s = null;
JLabel label=null;
JTextField text;
JTextArea text1;

public SendMessage(String ip,int i,JTextArea text1) {
// TODO Auto-generated constructor stub
this.ip=ip;
this.i=i;
this.text1=text1;

}


public void run(){

while(true){
try {
s = new Socket(ip,i);
text1.setText("连接成功"+"\n");
break;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
System.out.println("出错了。。。。");
}

}

}

public void send(String message)
{
try {



PrintStream p = new PrintStream(s.getOutputStream());

p.println(message);


} catch (Exception e1) {
System.out.println("异常"+e1.getMessage());
}
}
 
 
}


服务器端:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JTextArea;


public class GetMessage extends Thread{
private int i;
String v;

JLabel label=null;
private JTextArea text;
Date d=new Date();
SimpleDateFormat matter=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
public GetMessage(int i,JTextArea text) {

this.i=i;
this.text=text;
}

public void run(){
try {
ServerSocket so = new ServerSocket(i);
Socket s = so.accept();
while(true){
InputStreamReader i = new InputStreamReader(s.getInputStream());
  相关解决方案