当前位置: 代码迷 >> J2SE >> 流和网络的有关问题,初学者!
  详细解决方案

流和网络的有关问题,初学者!

热度:212   发布时间:2016-04-24 15:21:06.0
流和网络的问题,菜鸟!!!!!!!!!!
初学者,有如下问题:

{1}流到底应该怎么理解;(最好说说各位高手自己的理解,形象一点别照资料上说),本来我已经理解了,在

学流时,我的理解是,可是昨天开始讲了网络编程后我又不明白了,

{2}对于网络编程,下面这两个程序应该怎么运行啊!!!!(在命令提示符下),先运行服务器端,一直在那里

等待,运行客户端又找不到网络.
   

{3}谢谢各位大虾!!!!!!!!!!!!!!!!!!!!!!


------解决方案--------------------
我这里有个小程序,不过有点小问题,必须客户端发过去后,服务端回.这样一来一回才行.你参考着看吧.
客户端:
import java.io.*;
import java.net.*;
public class Client {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Socket socket = new Socket( "192.168.1.119 ", 4710);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readLine;
readLine = sin.readLine();
while(!readLine.equals( "bye "))
{
os.println(readLine);
os.flush();
System.out.println( "Client: " + readLine);
System.out.println( "Server: " + is.readLine());
readLine = sin.readLine();
}
os.close();
is.close();
socket.close();
}
catch(Exception e)
{
System.out.println( "Error " + e);
}
}

}
服务器:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)
{
try
{
ServerSocket server = null;
try
{
server = new ServerSocket(4710);
}
catch(Exception e)
{
System.out.println( "can not listen t " + e);
}
Socket socket = null;
try
{
socket = server.accept();
}
catch(Exception e){
System.out.println( "Error. " + e);
}
String line;
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
System.out.println( "Client: " + is.readLine());
line = sin.readLine();
while(!line.equals( "bye "))
{
os.println(line);
os.flush();
System.out.println( "Server: " + line);
System.out.println( "Client: " + is.readLine());
line = sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
}
catch(Exception e){
System.out.println( "Error: " + e);
}
}
}
  相关解决方案