java tcp接受数据

haotainan 2011-11-05 08:02:03

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,说明只循环了一次,这是怎么搞的?
...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
haotainan 2011-11-06
  • 打赏
  • 举报
回复
因为数据太大,我是把一个数据分成好几份发送,在接收端也一个一个地读取到一个输出流里,这样的话怎么搞的额?
chabale 2011-11-06
  • 打赏
  • 举报
回复
客户端:
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());
BufferedReader b = new BufferedReader(i);
v= b.readLine();
text.append(String.valueOf(matter.format(d))+"\n");
text.append("对方说"+v+"\n");
}
} catch (IOException e) {
System.out.println(e.getMessage());
//label.setText("对方已经下线");
text.append("对方下线了。。。");
}
}
}
xiazdong 2011-11-06
  • 打赏
  • 举报
回复
建议:
1.Socket输入输出流包装:
PrintWriter包装s.getOutputStream();
BufferedReader包装s.getInputStream();
2.可以用多线程方法使得客户端能够同时访问;

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){}

}

}
pcmlose 2011-11-05
  • 打赏
  • 举报
回复
socket=ss.accept(); //堵塞
接受到客户端一次就运行一次,Server要支持多个客户端。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧