java Socket编程中read方法的阻塞问题,求高手解答 代码如下
importjava.io.*;importjava.net.*;classClientDemo{publicstaticvoidmain(String[]args)th...
import java.io.*;
import java.net.*;
class ClientDemo
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",10002);
DataOutputStream out =new DataOutputStream(s.getOutputStream());
out.write("TCP 来了".getBytes());
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println("server:"+text);
s.close();
}
}
class ServerDemo
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();//该方法是阻塞式的。
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
while(( len = in.read(buf))!=-1);
{
String text = new String(buf,0,len);
System.out.println(text);
}
DataOutputStream out =new DataOutputStream(s.getOutputStream());
out.write("已收到".getBytes());
s.close();
ss.close();
}
} 展开
import java.net.*;
class ClientDemo
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",10002);
DataOutputStream out =new DataOutputStream(s.getOutputStream());
out.write("TCP 来了".getBytes());
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println("server:"+text);
s.close();
}
}
class ServerDemo
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();//该方法是阻塞式的。
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
while(( len = in.read(buf))!=-1);
{
String text = new String(buf,0,len);
System.out.println(text);
}
DataOutputStream out =new DataOutputStream(s.getOutputStream());
out.write("已收到".getBytes());
s.close();
ss.close();
}
} 展开
2个回答
展开全部
import java.io.*;
import java.net.*;
class ClientDemo {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 10002);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.write("TCP 来了".getBytes());
out.flush();
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
while (true) {
if ((len = in.read(buf)) != -1) {
String text = new String(buf, 0, len);
System.out.println("server:" + text);
break;
}
}
s.close();
}
}
class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10002);
while (true) {
Socket s = ss.accept();// 该方法是阻塞式的。
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
if ((len = in.read(buf)) != -1) {
String text = new String(buf, 0, len);
System.out.println(text);
out.write("已收到".getBytes());
out.flush();
}
}
}
}
给你改了下
import java.net.*;
class ClientDemo {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 10002);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.write("TCP 来了".getBytes());
out.flush();
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
while (true) {
if ((len = in.read(buf)) != -1) {
String text = new String(buf, 0, len);
System.out.println("server:" + text);
break;
}
}
s.close();
}
}
class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10002);
while (true) {
Socket s = ss.accept();// 该方法是阻塞式的。
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
if ((len = in.read(buf)) != -1) {
String text = new String(buf, 0, len);
System.out.println(text);
out.write("已收到".getBytes());
out.flush();
}
}
}
}
给你改了下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询