TCP/IP基礎知識&Oracle的TCP/IP網路應用

junnyblog發表於2009-10-16
主要總結TCP/IP基礎知識,同時結合Oracle的網路連線進行講訴TCP/IP的應用.....整理中.....[@more@]

//MyServer

import java.io.*;
import java.net.*;

import com.common.*;

public class MyServer {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//建立ServerS0cket
// System.out.println("i am listening!");
// ServerSocket ss=new ServerSocket(8888);
// Socket s=ss.accept();
// InputStreamReader isr=new InputStreamReader(s.getInputStream());
// BufferedReader br=new BufferedReader(isr);
// String kk=br.readLine();
// System.out.println("client send :"+kk);
//
// OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());
// PrintWriter pw=new PrintWriter(osw,true);
// pw.println(kk+" two!");


//物件流
System.out.println("我是伺服器,在9999監聽");
ServerSocket ss=new ServerSocket(9999);
Socket s=ss.accept();

//接收客戶端發來的資訊.

ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
User u=(User)ois.readObject();
System.out.println("伺服器接收到使用者id:"+u.getUserName()+" 密碼:"+u.getPasswd());


} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

}

//MyClien

import java.io.*;
import java.net.*;
public class MyClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
Socket s=new Socket("127.0.0.1",8888);
OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());
BufferedWriter bw=new BufferedWriter(osw);
PrintWriter pw=new PrintWriter(bw,true);
pw.println("hello,world!");

InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
String kk=br.readLine();

System.out.println("myserver send :"+kk);

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

//common

package com.common;

public class User implements java.io.Serializable{
String userName;
String passwd;
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22630256/viewspace-1027925/,如需轉載,請註明出處,否則將追究法律責任。

相關文章