先贴一段程序.很简单的,有几个问题要请教大家,谢谢实现Client端功能的ClientApp.java原文件:import java.net.*;
import java.io.*;
import java.lang.*;public class ClientApp
{
public static void main(String args[])
{
try
{
//创建通讯并且和主机Rock连接
Socket cSocket=new Socket("192.168.100.188",8018);
//打开这个Socket的输入/输出流
OutputStream os=cSocket.getOutputStream();
DataInputStream is=new DataInputStream(cSocket.getInputStream()); int c;
boolean flag=true; String responseline; while(flag)
{
//从标准输入输出接受字符并且写如系统
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
//将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close(); }
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
实现Server端功能的ServerApp.java原文件:import java.net.*;
import java.io.*;public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c; ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort()); while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream(); while((inputLine=is.readLine())!=null)
{
//当客户端输入stop的时候服务器程序运行终止!
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine); while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush(); //将信息发送到客户端
break;
}
}
}
}
is.close();
os.close();
clientSocket.close(); }
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
import java.io.*;
import java.lang.*;public class ClientApp
{
public static void main(String args[])
{
try
{
//创建通讯并且和主机Rock连接
Socket cSocket=new Socket("192.168.100.188",8018);
//打开这个Socket的输入/输出流
OutputStream os=cSocket.getOutputStream();
DataInputStream is=new DataInputStream(cSocket.getInputStream()); int c;
boolean flag=true; String responseline; while(flag)
{
//从标准输入输出接受字符并且写如系统
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
//将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close(); }
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
实现Server端功能的ServerApp.java原文件:import java.net.*;
import java.io.*;public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c; ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort()); while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream(); while((inputLine=is.readLine())!=null)
{
//当客户端输入stop的时候服务器程序运行终止!
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine); while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush(); //将信息发送到客户端
break;
}
}
}
}
is.close();
os.close();
clientSocket.close(); }
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
os.write((byte)c);
if(c=='\n')
这三个语句分别是什么意思?我看不懂还有,OutputStream os=cSocket.getOutputStream();有这样的语句,但OutputStream不是一个抽象类吗?抽象类不是不可以建立对象的么?
为什么这里会是这样?问题比较菜,请教啦
Outpustream是抽象类,是不可以建立对象的,
但这里cSocket.getOutputStream()并不是真的建立OutputStream对象,
而是返回一个OutputStream的子类,用OutputStream来引用而已,
父类是可以引用子类的,当然其子类一般不会是抽象类,
public OutputStream getOutputStream() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isConnected())
throw new SocketException("Socket is not connected");
if (isOutputShutdown())
throw new SocketException("Socket output is shutdown");
final Socket s = this;
OutputStream os = null;
try {
os = (OutputStream) //*********看这里你就会容易明白一些
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IOException {
return impl.getOutputStream();
}
});
} catch (java.security.PrivilegedActionException e) {
throw (IOException) e.getException();
}
return os;
}
你这是什么?是不是就是那个引用?
是不是JAVA语言中已经隐含了的内容?
那这os是一个什么类的变量呢?
你这是什么?是不是就是那个引用?
是不是JAVA语言中已经隐含了的内容?》这是Socket的类定义中的getOutputStream()方法的源代码,你多看些类扩展和继承就可以
这不是一句两句可以说明白的
public abstarct class First{}//一个抽象类
public class Second extends First
{
}
这分别是两个类的定义,First相当于OutputStream,而Second相当于OutputStream的之类
然后在其他类中应用
public class Test
{
public void use()
{ First a=this.getFirst();//
} public static First getFirst()
{
Second a=new Second();
return (First)a;//实际返回的a是First的子类Second类
}
}