javacto 发表于 2013-1-28 19:30:48

网络学习【017】

-------1----------------------------------------------------------------------------------
// TCP example onepackage com.testnet;import java.net.* ;import java.io.* ;public class TCPServer {public static void main(String args[]) throws Exception {ServerSocket ss = new ServerSocket(5566);while (true) {Socket as = ss.accept();DataInputStream dis = new DataInputStream(as.getInputStream()) ;System.out.println(dis.readUTF()) ;//System.out.println("A client connect!");dis.close() ;as.close();}}}
package com.testnet;import java.io.*;import java.net.* ;public class TCPClinet {public static void main(String args[])throws Exception {Socket s = new Socket("127.02.0.1", 5566) ;OutputStream os = s.getOutputStream() ;DataOutputStream dos = new DataOutputStream(os) ;dos.writeUTF("hello server !") ; //以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流dos.flush();dos.close();s.close();}}

package com.testnet;import java.io.*;import java.net.* ;public class TCPClinet1 {public static void main(String args[])throws Exception {Socket s = new Socket("127.02.0.1", 6655) ;OutputStream os = s.getOutputStream() ;DataOutputStream dos = new DataOutputStream(os) ;Thread.sleep(30000) ;dos.writeUTF("hello everyone !") ; //以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流dos.flush();dos.close();s.close();}}

--2----------------------------------------------------------------------------------
package testnet1;import java.io.* ;import java.net.* ;public class TestClient {public static void main(String args[]) {try {Socket soc = new Socket("127.02.0.1",8888) ;DataInputStream dis = new DataInputStream(soc.getInputStream()) ;System.out.println(dis.readUTF()) ;soc.close();dis.close();} catch(ConnectException cone) {cone.printStackTrace();System.out.println("Server connect failure") ;} catch (IOException e) {e.printStackTrace() ;}}}

package testnet1;import java.io.* ;import java.net.* ;public class TestServer {public static void main(String args[]) {try {ServerSocket ss = new ServerSocket(8888) ;while(true) {Socket s1 = ss.accept();DataOutputStream dos = new DataOutputStream(s1.getOutputStream()) ;dos.writeUTF("HELLO," + s1.getInetAddress() + " port#" + s1.getPort() + "Bye-bye") ;dos.close();s1.close();}} catch (IOException e) {e.printStackTrace() ;System.out.println("Program running error") ;}}}

-------3---------------------------------------------------------------------------------------
package testnet2;import java.io.* ;import java.net.* ;public class TestServer {public static void main(String args[]) {InputStream in = null ;OutputStream out = null ;try {ServerSocket ss = new ServerSocket(2382) ;      Socket so = ss.accept();      DataOutputStream dos = new DataOutputStream(so.getOutputStream()) ;      DataInputStream dis = new DataInputStream(so.getInputStream()) ;      String s = null ;      if((s=dis.readUTF()) != null) {      System.out.println(s) ;      System.out.println("From:" + so.getInetAddress()) ; //此套接字连接到的远程 IP 地址;如果套接字是未连接的,则返回 null      System.out.println("Fort:" + so.getPort()) ; //返回此套接字连接到的远程端口      }      dos.writeUTF("hi, hello") ;      dos.close() ;      dis.close();      so.close();} catch(IOException e) {e.printStackTrace();}}}
package testnet2;import java.io.* ;import java.net.* ;public class TestClinet {public static void main(String args[]) {InputStream is = null ;OutputStream os = null ;try {Socket s = new Socket("192.168.1.104",2382) ;DataInputStream dis = new DataInputStream(s.getInputStream()) ;DataOutputStream dos = new DataOutputStream(s.getOutputStream()) ;dos.writeUTF("hey") ;String st = null ;if((st=dis.readUTF()) != null){System.out.println(st) ;dis.close() ;dos.close() ;s.close() ;}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
-----4----------------------------------------------------------------------------------
package testudp1;import java.io.IOException;import java.net.* ;public class TestServer {public static void main(String args[]) {byte buf[] = new byte ;DatagramPacket dp = new DatagramPacket(buf,buf.length) ; //用于接收对方的数据DatagramSocket ds = null ; try {ds = new DatagramSocket(5678) ; // udp 的5678 端口} catch (SocketException e) {e.printStackTrace();}while(true) {try {ds.receive(dp) ; //从此套接字接收数据报包dp} catch (IOException e) {e.printStackTrace();}System.out.println(new String(buf,0,dp.getLength())) ; //dp.getLength()得到实际收到的数据}}}
package testudp1;import java.io.IOException;import java.net.* ;public class TestClient {public static void main(String args[]) {byte[] buf = (new String("Hello")).getBytes(); //把字符串解析成字符数组try {DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678)) ; //构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号;SocketAddress包含ip和端口 DatagramSocket ds = new DatagramSocket(9999) ; //创建数据报套接字,将其绑定到指定的本地地址try {ds.send(dp) ;//从此套接字发送数据报包dpds.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
------5----------------------------------------------------------------------------------
package testudp2;import java.io.* ;import java.net.* ;public class TestUDPServer {public static void main(String args[]) {byte buf[] = new byte ;DatagramPacket dp = new DatagramPacket(buf,buf.length) ; //用于接收对方的数据DatagramSocket ds = null ; try {ds = new DatagramSocket(5678) ; // udp 的5678 端口} catch (SocketException e) {e.printStackTrace();}while(true) {try {ds.receive(dp) ; //从此套接字接收数据报包dp} catch (IOException e) {e.printStackTrace();}ByteArrayInputStream bis = new ByteArrayInputStream(buf);DataInputStream dis = new DataInputStream(bis) ;try {System.out.println(dis.readLong()) ;} catch (IOException e) {e.printStackTrace();}}}}
package testudp2;import java.io.ByteArrayOutputStream;import java.io.* ;import java.net.* ;public class TestUDPClient {public static void main(String args[]) {//byte[] buf = (new String("Hello")).getBytes(); //把字符串解析成字符数组try {long n = 1000L ;ByteArrayOutputStream baos = new ByteArrayOutputStream() ;DataOutputStream dos = new DataOutputStream(baos) ;dos.writeLong(n) ;byte[] buf = baos.toByteArray() ;System.out.println(buf.length) ;DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678)) ; //构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号;SocketAddress包含ip和端口 DatagramSocket ds = new DatagramSocket(9999) ; //创建数据报套接字,将其绑定到指定的本地地址try {ds.send(dp) ;//从此套接字发送数据报包dpds.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace(); } catch(IOException e) {e.printStackTrace() ;}}}

-------6------------------------------------------------------------------------------------

package TestOne;import java.io.* ;import java.net.* ;public class TalkServer {public static void main(String args[]) {try {ServerSocket server = null ;try {server = new ServerSocket(4700) ;} catch (IOException e) {System.out.println("cann't listen to:" + e) ;System.exit(-1) ;}Socket socket = server.accept();BufferedReader is = new BufferedReader (new InputStreamReader(socket.getInputStream())) ; //输入通道PrintWriter os = new PrintWriter(socket.getOutputStream()) ;//输出通道BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)) ;//键盘System.out.println("Client" + is.readLine()) ;String line = sin.readLine();while(!line.equalsIgnoreCase("exit") && !line.equalsIgnoreCase("bye")) {os.println(line) ; // 写到客户端os.flush() ;System.out.println("-----Server:" + line) ;System.out.println("Client:"+ is.readLine()) ;line = sin.readLine();}is.close();os.close();sin.close();server.close();} catch(Exception e) {System.out.println("Error." ) ;}}}
package TestOne;import java.io.* ;import java.net.* ;public class TalkClient {public static void main(String args[]) {try {Socket socket = new Socket("127.0.0.1",4700) ;BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)) ;PrintWriter os = new PrintWriter(socket.getOutputStream()) ;BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())) ;String readline = sin.readLine();while(!readline.equalsIgnoreCase("bay") && !readline.equalsIgnoreCase("exit")) ; {os.println(readline) ;os.flush();System.out.println("----Client:" + readline) ;readline = sin.readLine();}os.close();is.close();socket.close() ;} catch (Exception e) {System.out.println("Error" + e) ;System.exit(-1) ;}}}
页: [1]
查看完整版本: 网络学习【017】