java socket问题,写了个简单的程序,客户端输出到服务器端,服务器端接收到后返回数据,出现问题!!
server端:ServerSocketss=null;Socketsocket=null;try{ss=newServerSocket(8888);System.out...
server端:
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
BufferedReader br=new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
System.out.println(br.readLine());
bw.write(br.readLine()+"received");
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
client端:
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
BufferedReader br=new BufferedReader(new InputStreamReader(is));
bw.write("hello");
System.out.println(br.readLine());
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端的数据传不到服务器端,服务器端的数据不能返回到客户端 展开
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
BufferedReader br=new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
System.out.println(br.readLine());
bw.write(br.readLine()+"received");
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
client端:
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
BufferedReader br=new BufferedReader(new InputStreamReader(is));
bw.write("hello");
System.out.println(br.readLine());
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端的数据传不到服务器端,服务器端的数据不能返回到客户端 展开
2个回答
展开全部
我修改了,直接用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
服务器端:
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是过期的啊,我要读一行数据还是不太方便~~~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询