java socket问题,写了个简单的程序,客户端输出到服务器端,服务器端接收到后返回数据,出现问题!!
答案:2 悬赏:70 手机版
解决时间 2021-11-11 08:40
- 提问者网友:遁入空寂
- 2021-11-11 01:01
java socket问题,写了个简单的程序,客户端输出到服务器端,服务器端接收到后返回数据,出现问题!!
最佳答案
- 五星知识达人网友:迟山
- 2021-11-11 02:37
我修改了,直接用socket的输入输出流实现:
服务器端:
import java.io.*;
import java.net.*;
public class server{
public static void main(String args[]) throws IOException
{
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
System.out.println("New connection accepted " +socket.getInetAddress() + ":" +socket.getPort());
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
String recvStr = DIS.readLine();
System.out.println(recvStr);
PS.println(recvStr+"received");
DIS.close();
PS.close();
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.*;
import java.net.*;
public class client{
public static void main(String args[]) throws IOException
{
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
if(socket == null)
System.out.println("error");
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
PS.println("hello");
System.out.println(DIS.readLine());
DIS.close();
PS.close();
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
java server
server is starting
New connection accepted /127.0.0.1:45786
hello
java client
helloreceived追问这样写我这儿也没问题,问题是这个类得readline是过期的啊,我要读一行数据还是不太方便~~~
服务器端:
import java.io.*;
import java.net.*;
public class server{
public static void main(String args[]) throws IOException
{
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
System.out.println("New connection accepted " +socket.getInetAddress() + ":" +socket.getPort());
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
String recvStr = DIS.readLine();
System.out.println(recvStr);
PS.println(recvStr+"received");
DIS.close();
PS.close();
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.*;
import java.net.*;
public class client{
public static void main(String args[]) throws IOException
{
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
if(socket == null)
System.out.println("error");
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
PS.println("hello");
System.out.println(DIS.readLine());
DIS.close();
PS.close();
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
java server
server is starting
New connection accepted /127.0.0.1:45786
hello
java client
helloreceived追问这样写我这儿也没问题,问题是这个类得readline是过期的啊,我要读一行数据还是不太方便~~~
全部回答
- 1楼网友:撞了怀
- 2021-11-11 03:11
嗯,问题处在了楼主没有调用bw.flush()方法,数据还都在内存里呢还没写到流中,当然看不到了,应该在server和client的bw.close()上面各加bw.flush()就Ok了追问flush都加过了,还是不行,如果单独在client端写,在server端读,完全没有问题,但是在两端同时写和读就有问题
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯