网络学习【017】

-------1----------------------------------------------------------------------------------
[code]
// TCP example one

package 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();
}
}
}

[/code]
[code]
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();
}

}

[/code]

[code]
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();
}
}

[/code]

--2----------------------------------------------------------------------------------
[code]
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() ;
}
}

}

[/code]

[code]
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") ;
}
}

}

[/code]

-------3---------------------------------------------------------------------------------------
[code]
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();
}
}

}

[/code]
[code]
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 block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
[/code]
-----4----------------------------------------------------------------------------------
[code]
package testudp1;
import java.io.IOException;
import java.net.* ;
public class TestServer {
public static void main(String args[]) {
byte buf[] = new byte[1024] ;
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()得到实际收到的数据
}
}

}

[/code]
[code]
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) ; //从此套接字发送数据报包dp
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

[/code]
------5----------------------------------------------------------------------------------
[code]
package testudp2;
import java.io.* ;
import java.net.* ;
public class TestUDPServer {
public static void main(String args[]) {
byte buf[] = new byte[1024] ;
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();
}

}
}

}
[/code]
[code]
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) ; //从此套接字发送数据报包dp
ds.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace() ;
}

}

}
[/code]

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

[code]
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." ) ;
}
}
}
[/code]
[code]
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) ;
}
}

}

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值