Java三章学习内容(基于TCP,UDP协议socket)


TCP UDP
是否连接 面向连接 面向非连接
传输可靠性 可靠 不可靠
速度
socket编程基于java.net包

一.基于TCP协议socket
有服务器端
ServerSocket
客户端Socket
localhost代表本机

使用InetAddress 获取ip地址代码:InetAddress is = socket.getInetAddress();            
String ip = is.getHostAddress();


ServerSocket serverSocket = new ServerSocket(5000); //新建服务器socket
Socket socket = serverSocket.accept(); //使用socket.accept方法监听用户,返回值是socket
InputStream is =socket.getInputStream(); //socket.get方法get出InputStream
socket.shutdownOutput();                              //关闭相应流

服务器代码:
public class Server {
public static void main(String[] args) {
try {
Scanner  scanner = new Scanner(System.in);
ServerSocket serverSocket = new ServerSocket(5000); //新建服务器socket
Socket socket = serverSocket.accept(); //使用socket.accept方法监听用户,返回值是socket

OutputStream os  =socket.getOutputStream();

InputStream is =socket.getInputStream(); //socket.get方法get出InputStream
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String fwqstr=null;
while ((fwqstr=br.readLine())!=null) {
System.out.println(fwqstr);
}
socket.shutdownInput();

boolean flag = false;
while (flag) {
System.out.println("客服代表:");
String server = scanner.next();
os.write(server.getBytes());
socket.shutdownOutput();
}
socket.shutdownOutput();


if (os!=null) {
os.close();
}


if (br!=null) {
br.close();
}
if (is!=null) {
is.close();
}
if (socket!=null) {
socket.close();
}
if (serverSocket!=null) {
serverSocket.close();
}


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

}
}

客户端代码:
public class Client {
public static void main(String[] args) {
try {
Scanner scanner = new  Scanner(System.in);
Socket socket = new Socket("localhost",5000); //新建socket,连接本机,端口5000
OutputStream os =socket.getOutputStream(); //socket.get方法得到相应的流
boolean flag = false;
while (!flag) {
System.out.print("玩家:");
String user=scanner.next();
os.write(user.getBytes());
socket.shutdownOutput();
if (user.equals("0")) {
break;
}
}
InputStream is =socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String khdstr1=null;
while ((khdstr1=br.readLine())!=null) {
System.out.println(khdstr1);
}
socket.shutdownInput();
if (br!=null) {
br.close();
}

if (is!=null) {
is.close();
}
if (os!=null) {
os.close();
}
if (socket!=null) {
socket.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

二.基于UDP协议socket
DatagramPacket   对象封装数据包
DatagramSocket   发送数据包
DatagramSocket   接收数据包
DatagramPacket    处理数据包
DatagramSocket---------有.send发送方法和.receive接受方法

参考代码:
客户端:
public class LoginClient {
public static void main(String[] args) {
try {
//创建需要发送服务器的信息
String info="账号:[email protected]";

//使用InetAddress获得地址
InetAddress ia =InetAddress.getByName("localhost");

//使用Datagrampacket包装信息
DatagramPacket dp = new DatagramPacket(info.getBytes(), info.getBytes().length, ia, 5000);

//使用datagramsocket寄出物品
DatagramSocket ds = new DatagramSocket();
ds.send(dp);


byte[] b = new byte[1024];
DatagramPacket dp2 = new DatagramPacket(b, b.length);

ds.receive(dp2);
String info2 = new String(dp2.getData(),0,dp2.getData().length);
System.out.println(info2);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器:
public class LoginServer {
public static void main(String[] args) {
try {
//拿数组接受并且通过datagrampacket包装
byte [] infos = new byte[1024];
DatagramPacket dp = new DatagramPacket(infos,infos.length);

//接受客户端的信息receive

DatagramSocket ds = new DatagramSocket(5000);
ds.receive(dp);
String info = new String(dp.getData(),0,dp.getData().length);
System.out.println(info);

String str="欢迎您的登录";
byte[] replys = str.getBytes();
SocketAddress is =dp.getSocketAddress();
DatagramPacket dp1 = new DatagramPacket(replys, replys.length, is);
ds.send(dp1);

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

}
}


猜你喜欢

转载自blog.csdn.net/jayvergil/article/details/80300712