java 输入输出与socket

//一个创建socket的例子
import java.net.*;
import java.io.*;

class server
{
    
public static void main(String args[]){
        
try{
            ServerSocket SS1
=new ServerSocket(1234);
            System.out.println(
"Serversocket is created!");
            System.out.println(
"SS1.getLocalPort()="+SS1.getLocalPort());
            
//SS1.close();
            System.out.println("SS1 is closed");

            ServerSocket ss2
=new ServerSocket(0,2);
            System.out.println(
"serversocket ss2 is created");
            System.out.println(
"SS2.getLocalPort()="+ss2.getLocalPort());
            ss2.close();
            System.out.println(
"ss2 is closed");
        }
     
catch(IOException e)
        {
         System.out.println(e.getMessage());
        }
    }

}

第二个例子
//一个读文件属性的例子
import java.io.File;
import java.util.Date;
class file_objects{
    
public static void main(String args[]) throws java.io.IOException{
        
if (args.length<1)
        {
            System.out.println(
"Usage:java file_objects[]");
            System.exit(
1);
        }

        String fileName
=args[0];
        File f
=new File(fileName);

        System.out.println(
"the file is ");
        System.out.println(
"File name:"+f.getName());
        System.out.println(
"File name:"+f.getName());
        System.out.println(
"Canonical File Name:"+f.getCanonicalFile());
        System.out.println(
"can writeable:"+f.canWrite());
        System.out.println(
"length:"+f.length());
        System.out.println(
"Hidden:"+f.isHidden());
        System.out.println(
"isFile:"+f.isFile());
        System.out.println(
"isDirectory:"+f.isDirectory());
        System.out.println(
"LastModified:"+new Date(f.lastModified()));
    }
}

以下为一server&client 的例子:

以下为
import java.net.*;
import java.io.*;

public class server {
 
public static void main(String[] args) {
  Socket socket
=null;
  BufferedReader br
=null;
  PrintWriter pw
=null;
  
try {
   
//创建服务器,并开放3081端口
   ServerSocket server=new ServerSocket(3081);
   
while(true){
    
    
//监听服务器端口,一旦有数据发送过来,那么就将数据封装成socket对象
    
//如果没有数据发送过来,那么这时处于线程阻塞状态,不会向下继续执行
    socket=server.accept();
    System.out.println(
"客户端信息:"+socket.getRemoteSocketAddress());
    
//从socket中得到读取流,该流中有客户端发送过来的数据
    InputStream in=socket.getInputStream();
    
//InputStreamReader将字节流转化为字符流
    br=new BufferedReader(new InputStreamReader(in));
    
//行读取客户端数据
    String info=br.readLine();
    System.out.println(info);
    
    
    OutputStream out
=socket.getOutputStream();
    pw
=new PrintWriter(out);
    pw.println(
"服务器说:hello,everyone,I love you ");
    pw.flush();
   }
   
  } 
catch (Exception e) {
   
// TODO Auto-generated catch block
   e.printStackTrace();
  }
finally{
   
   
try {
    pw.close();
    br.close();
    socket.close();
   } 
catch (IOException e) {
    
// TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }
}
import java.net.*;
import java.io.*;

public class client {
    
public static void main(String[] args) {
  Socket socket
=null;
  PrintWriter pw
=null;
  BufferedReader br
=null;
  
try {
   
//创建socket对象,并指明服务器的IP地址和端口号
   socket=new Socket("localhost",3081);
   
//得到socket发送数据的输出流
   OutputStream out=socket.getOutputStream();
   
//将字节流包装成字符流
   pw=new PrintWriter(out);
   
   
//向服务器发送数据
   pw.println("客户端说:hi");
   
//刷新流,确保数据能写到服务器
   pw.flush();
   
   InputStream in
=socket.getInputStream();
   
   br
=new BufferedReader(new InputStreamReader(in));
   String info
=br.readLine();
   System.out.println(info);
   } 
catch (Exception e) {
   
// TODO Auto-generated catch block
   e.printStackTrace();
  }
  
finally{
   
try {
    pw.close();
    socket.close();
   } 
catch (IOException e) {
    
// TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

服务端:
D:\javasource>java server
客户端信息:/127.0.0.1:49589
客户端说:hi
客户端:

D:\javasource>netstat -an|find "3081"
  TCP    0.0.0.0:3081           0.0.0.0:0              LISTENING
  TCP    [::]:3081              [::]:0                 LISTENING

D:\javasource>java client
服务器说:hello,everyone,I love you

jdbc的例子:
顺带一个
/*
Java Programming with Oracle JDBC
by Donald Bales 
ISBN: 059600088X
Publisher: O'Reilly
*/


/*
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
*/

import java.sql.*;

public class jdbc {

  
public static void main(String args[]) {

    
try {
      Class.forName(
"oracle.jdbc.driver.OracleDriver");
    }
 catch (ClassNotFoundException e) {
      System.out
          .println(
"Oops! Can't find class oracle.jdbc.driver.OracleDriver");
      System.exit(
1);
    }


    Connection conn 
= null;
    Statement stmt 
= null;
    ResultSet rset 
= null;
    
try {
      conn 
= DriverManager.getConnection(
          
"jdbc:oracle:thin:@192.168.100.203:1521:instancename""username""password");

      stmt 
= conn.createStatement();
      rset 
= stmt
          .executeQuery(
"select 'Hello '||USER||'!' result from dual");
      
while (rset.next())
        System.out.println(rset.getString(
1));
      rset.close();
      rset 
= null;
      stmt.close();
      stmt 
= null;
      conn.close();
      conn 
= null;
    }
 catch (SQLException e) {
      System.out.println(
"Darn! A SQL error: " + e.getMessage());
    }
 finally {
      
if (rset != null)
        
try {
          rset.close();
        }
 catch (SQLException ignore) {
        }

      
if (stmt != null)
        
try {
          stmt.close();
        }
 catch (SQLException ignore) {
        }

      
if (conn != null)
        
try {
          conn.close();
        }
 catch (SQLException ignore) {
        }

    }

  }

}



这样是不行的.

D:\temp>java -classpath d:\temp\ojdbc6.jar jdbc
错误: 找不到或无法加载主类 jdbc

D:\temp>java -classpath d:\temp\ojdbc6.jar;. jdbc
或者改环境变量:set classpath=%classpath%;.;d:\temp\ojdbc6.jar;%JRE_HOME%\lib\dt.jar
注:linux 下是java -classpath /root/ojdbc6.jar:. jdbc
或者export CLASSPATH=.:/home/ojdbc14.jar


posted on 2011-11-16 23:39 snowhill 阅读(336) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜