当前位置: 技术问答>java相关
jbulider编程中的socket问题(我只有这些分了)
来源: 互联网 发布时间:2015-03-04
本文导语: 小弟用jbuilder5.0学习java网络编程,编了一个c/s程序, server用 ServerSocket soc=new ServerSocket(8000); Socket server_socket=soc.accept(); DataInputStream in=new DataInputStream(server_socket.getInputStream()); DataOutputStream out=new DataOutputStrea...
小弟用jbuilder5.0学习java网络编程,编了一个c/s程序,
server用
ServerSocket soc=new ServerSocket(8000);
Socket server_socket=soc.accept();
DataInputStream in=new DataInputStream(server_socket.getInputStream());
DataOutputStream out=new DataOutputStream(server_socket.getOutputStream());
client端用
Socket client_socket=new Socker("localhost",8000);
DataInputStream in=new DataInputStream(client_socket.getInputStream());
DataOutputStream out=new DataOutputStream(client_socket.getOutputStream());
二者可以连接上,但是当client段调用out.writeUTF(string),
server端调用in.readUTF(string)时,
为何只有当client端调用两次out.writeUTF(string)时,server端
才能收到一次数据,而且时两次传输中前面的那一次的数据。
这究竟是为什么?
server用
ServerSocket soc=new ServerSocket(8000);
Socket server_socket=soc.accept();
DataInputStream in=new DataInputStream(server_socket.getInputStream());
DataOutputStream out=new DataOutputStream(server_socket.getOutputStream());
client端用
Socket client_socket=new Socker("localhost",8000);
DataInputStream in=new DataInputStream(client_socket.getInputStream());
DataOutputStream out=new DataOutputStream(client_socket.getOutputStream());
二者可以连接上,但是当client段调用out.writeUTF(string),
server端调用in.readUTF(string)时,
为何只有当client端调用两次out.writeUTF(string)时,server端
才能收到一次数据,而且时两次传输中前面的那一次的数据。
这究竟是为什么?
|
按你的要求,小弟做了这个程序,发送两次,均送到。试一试把
import java.io.*;
import java.net.*;
public class TestTCP{
public static void main(String args[]){
Server s = new Server(8000);
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
Client c = new Client("localhost",8000);
c.send("Hello");
c.send("World!");
s.print();
}
}
class Server implements Runnable{
private ServerSocket server;
private Socket client;
public Server(int port){
try{
System.out.println("constructing server...");
server = new ServerSocket(port);
Thread t = new Thread(this);
t.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void print(){
try{
BufferedReader br = new BufferedReader
(new InputStreamReader(client.getInputStream()));
String str;
str = br.readLine();
while(str!=null){
System.out.println("server get msg:""+str+""");
str = br.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
System.out.println("listening for client...");
client = server.accept();
}catch(Exception e){
e.printStackTrace();
}
}
}
class Client{
private Socket client;
public Client(String host,int port){
try{
System.out.println("Constructing client...");
client = new Socket(host,port);
}catch(Exception e){
e.printStackTrace();
}
}
public void send(String str){
try{
OutputStreamWriter osw = new OutputStreamWriter
(client.getOutputStream());
System.out.println("Client sending msg:""+str+""");
osw.write(str+"n");
osw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class TestTCP{
public static void main(String args[]){
Server s = new Server(8000);
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
Client c = new Client("localhost",8000);
c.send("Hello");
c.send("World!");
s.print();
}
}
class Server implements Runnable{
private ServerSocket server;
private Socket client;
public Server(int port){
try{
System.out.println("constructing server...");
server = new ServerSocket(port);
Thread t = new Thread(this);
t.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void print(){
try{
BufferedReader br = new BufferedReader
(new InputStreamReader(client.getInputStream()));
String str;
str = br.readLine();
while(str!=null){
System.out.println("server get msg:""+str+""");
str = br.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
System.out.println("listening for client...");
client = server.accept();
}catch(Exception e){
e.printStackTrace();
}
}
}
class Client{
private Socket client;
public Client(String host,int port){
try{
System.out.println("Constructing client...");
client = new Socket(host,port);
}catch(Exception e){
e.printStackTrace();
}
}
public void send(String str){
try{
OutputStreamWriter osw = new OutputStreamWriter
(client.getOutputStream());
System.out.println("Client sending msg:""+str+""");
osw.write(str+"n");
osw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
}