Commit d040cfc8 authored by txp's avatar txp

socket test

parents
Pipeline #119 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TestNet</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package com.bjsxt.test01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) throws UnknownHostException {
//InetAddress ia = new InetAddress();
InetAddress ia = InetAddress.getByName("192.168.216.1");
System.out.println(ia);
System.err.println(ia.getHostName());
System.err.println(ia.getHostAddress());
InetAddress ia2 = InetAddress.getByName("DESKTOP-BF3K39J");
System.out.println(ia2);
System.err.println(ia2.getHostName());
System.err.println(ia2.getHostAddress());
InetAddress ia3 = InetAddress.getByName("127.0.0.1");// 相当于本机的ip
System.out.println(ia3);
System.err.println(ia3.getHostName());
System.err.println(ia3.getHostAddress());
InetAddress ia4 = InetAddress.getByName("localhost");// 相当于本机的ip
System.out.println(ia4);
System.err.println(ia4.getHostName());
System.err.println(ia4.getHostAddress());
System.out.println("------");
InetAddress ia5 = InetAddress.getByName("192.168.216.2");// 相当于本机的ip
System.out.println(ia5);
System.err.println(ia5.getHostName());
System.err.println(ia5.getHostAddress());
}
}
package com.bjsxt.test01;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class Test2 {
public static void main(String[] args) throws UnknownHostException {
InetSocketAddress isa = new InetSocketAddress("192.168.216.1", 6666);
System.out.println(isa.getPort());
System.err.println(isa.getHostName());
System.err.println(isa.getAddress());
InetAddress ia = isa.getAddress();
System.out.println(ia.getHostName());
System.out.println(ia.getHostAddress());
}
}
package com.bjsxt.test02;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client { // 客户端
public static void main(String[] args) throws IOException {
System.out.println("客户端启动...");
/* 创建套接字 */
Socket s = new Socket(InetAddress.getByName("192.168.216.1"), 8888);
/* 我们感受到的应用层向外传送数据是:输出流 */
OutputStream os = s.getOutputStream();
/* 向外发送一句话 */
String str = "你好";
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(str);
/* 客户端要接收服务器的反馈信息 */
s.shutdownInput();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
/* 关闭流, 关闭Socket资源 */
dis.close();
is.close();
dos.close();
os.close();
s.close();
}
}
package com.bjsxt.test02;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server { // 服务器
public static void main(String[] args) throws IOException {
System.out.println("服务器启动...");
/* 利用套接字, 指定服务器的端口号 */
ServerSocket ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
Socket s = ss.accept();
/* 应用层还是用输入的IO流接收数据 */
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
System.out.println("客户端对我说:" + str);
/* 服务器端像客户端反馈一句话 */
s.shutdownInput();
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String str2 = "服务器端接收到您的消息!";
dos.writeUTF(str2);
/* 关闭流 */
dis.close();
is.close();
ss.close();
s.close();
}
}
package com.bjsxt.test03;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client { // 客户端
public static void main(String[] args) throws IOException {
System.out.println("客户端启动...");
/* 创建套接字 */
Socket s = new Socket(InetAddress.getByName("192.168.216.1"), 8888);
/* 我们感受到的应用层向外传送数据是:输出流 */
OutputStream os = s.getOutputStream();
/* 将本地图片先整到程序里来, 从程序中利用Socket提供的输出流整出去 */
FileInputStream fis = new FileInputStream(new File("E:/5dde211677ae9.jpg"));
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
int len = bis.read(b);// 一个字节往里读
while(len != -1) {
os.write(b, 0, len);
len = bis.read(b);
}
/* 客户端要接收服务器的反馈信息 */
s.shutdownOutput();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
/* 关闭流, 关闭Socket资源 */
dis.close();
is.close();
bis.close();
fis.close();
os.close();
s.close();
}
}
package com.bjsxt.test03;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server { // 服务器
public static void main(String[] args) throws IOException {
System.out.println("服务器端启动...");
/* 利用套接字, 指定服务器的端口号 */
ServerSocket ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
Socket s = ss.accept();
/* 应用层还是用输入的IO流接收数据 */
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("d:/222.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len = is.read(b);// 一个字节往里读
while(len != -1) {
fos.write(b, 0, len);
len = is.read(b);
}
/* 服务器端向客户端反馈一句话 */
s.shutdownInput();
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String str2 = "您上传图片成功!";
dos.writeUTF(str2);
/* 关闭流 */
dos.close();
os.close();
bos.close();
fos.close();
is.close();
s.close();
ss.close();
}
}
package com.bjsxt.test04;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client { // 客户端
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端启动...");
/* 创建套接字 */
Socket s = new Socket(InetAddress.getByName("192.168.216.1"), 8888);
/* 1.键盘录入账号和密码 */
Scanner sc = new Scanner(System.in);
System.out.println("账号:");
String name = sc.next();
System.out.println("密码:");
String password = sc.next();
/* 2.将上面的账号密码封装成一个对象 */
User user = new User(name, password);
/* 3.将这个对象发送给服务器端
* 我们感受到的应用层向外传送数据是:输出流 */
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(user);
/* 客户端要接收服务器的反馈信息 */
s.shutdownOutput();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
boolean flag = dis.readBoolean();
if(flag) {
System.out.println("登陆成功!");
} else {
System.out.println("登陆失败!");
}
/* 关闭流, 关闭Socket资源 */
dis.close();
is.close();
oos.close();
os.close();
sc.close();
s.close();
}
}
package com.bjsxt.test04;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Server { // 服务器
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
System.out.println("服务器启动...");
/* 利用套接字, 指定服务器的端口号 */
ServerSocket ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
Socket s = ss.accept();
/* 应用层还是用输入的IO流接收数据
* 接收客户端过来的对象 */
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
User u = (User)ois.readObject();
System.out.println(u);
/* 服务器端像客户端反馈结果 */
s.shutdownInput();
boolean flag;
if("txp".equals(u.getName()) && "123".equals(u.getPassword())) {
flag = true;
} else {
flag = false;
}
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeBoolean(flag);
/* 关闭流 */
dos.close();
os.close();
ois.close();
is.close();
ss.close();
s.close();
}
}
package com.bjsxt.test04;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}
}
package com.bjsxt.test05;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client { // 客户端
public static void main(String[] args) {
System.out.println("客户端启动...");
/* 创建套接字 */
Socket s = null;
Scanner sc = null;
String name = null;
OutputStream os = null;
ObjectOutputStream oos = null;
InputStream is = null;
DataInputStream dis = null;
try {
s = new Socket(InetAddress.getByName("192.168.216.1"), 8888);
/* 1.键盘录入账号和密码 */
sc = new Scanner(System.in);
System.out.println("账号:");
name = sc.next();
System.out.println("密码:");
String password = sc.next();
/* 2.将上面的账号密码封装成一个对象 */
User user = new User(name, password);
/* 3.将这个对象发送给服务器端
* 我们感受到的应用层向外传送数据是:输出流 */
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
oos.writeObject(user);
/* 客户端要接收服务器的反馈信息 */
s.shutdownOutput();
is = s.getInputStream();
dis = new DataInputStream(is);
boolean flag = dis.readBoolean();
if(flag) {
System.out.println("登陆成功!");
} else {
System.out.println("登陆失败!");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 关闭流, 关闭Socket资源 */
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.bjsxt.test05;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server { // 服务器
public static void main(String[] args) {
System.out.println("服务器启动...");
/* 利用套接字, 指定服务器的端口号 */
ServerSocket ss = null;
Socket s = null;
int count = 0;
try {
ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
while(true) {
s = ss.accept();
new ServerThread(s).start();
System.out.println("这是访问的第"+(++count)+"个用户,IP地址是:"+s.getInetAddress());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 关闭流 */
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.bjsxt.test05;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ServerThread extends Thread {
Socket s = null;
public ServerThread(Socket s) {
this.s=s;
}
InputStream is = null;
ObjectInputStream ois = null;
OutputStream os = null;
DataOutputStream dos = null;
@Override
public void run() {
try {
/* 应用层还是用输入的IO流接收数据
* 接收客户端过来的对象 */
is = s.getInputStream();
ois = new ObjectInputStream(is);
System.out.print(ois);
User u = (User)ois.readObject();
System.out.println(u);
/* 服务器端像客户端反馈结果 */
s.shutdownInput();
boolean flag;
if("txp".equals(u.getName()) && "123".equals(u.getPassword())) {
flag = true;
} else {
flag = false;
}
os = s.getOutputStream();
dos = new DataOutputStream(os);
dos.writeBoolean(flag);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
/* 关闭流 */
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.bjsxt.test05;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}
}
package com.txp.SocketTest01;
public class Client {
public void sendMsg(int i){
try{
System.out.println("͵"+i+"");
MySocketConnection.getSocket().getOutputStream().write(("͵"+i+"").getBytes("gbk"));
}catch(Exception e){
MySocketConnection.socket = null;
sendMsg(i);
}
}
}
package com.txp.SocketTest01;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) throws UnknownHostException {
Client c = new Client();
int i = 0;
while(true){
i ++ ;
c.sendMsg(i);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.txp.SocketTest01;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MySocketConnection {
public static Socket socket ;
public InetAddress ia;
public static Socket getSocket(){
try {
if(socket == null){
return new Socket(InetAddress.getByName("192.168.216.1"),12345);
}else{
return socket;
}
} catch (UnknownHostException e) {
} catch (IOException e) {
}
return null;
}
}
package com.txp.SocketTest02;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Connect {
private static final ThreadLocal<Socket> threadConnect = new ThreadLocal<Socket>();
private static final String HOST = "192.168.216.1";
private static final int PORT = 8888;
private static Socket client;
private static OutputStream outStr = null;
private static InputStream inStr = null;
private static Thread tRecv = new Thread(new RecvThread());
private static Thread tKeep = new Thread(new KeepThread());
public static void connect() throws UnknownHostException, IOException {
client = threadConnect.get();
if(client == null){
client = new Socket(HOST, PORT);
threadConnect.set(client);
tKeep.start();
System.out.println("========链接开始!========");
}
outStr = client.getOutputStream();
inStr = client.getInputStream();
}
public static void disconnect() {
try {
outStr.close();
inStr.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static class KeepThread implements Runnable {
public void run() {
try {
System.out.println("=====================开始发送心跳包==============");
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("发送心跳数据包");
outStr.write("send heart beat data package !".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class RecvThread implements Runnable {
public void run() {
try {
System.out.println("==============开始接收数据===============");
while (true) {
byte[] b = new byte[1024];
int r = inStr.read(b);
if(r>-1){
String str = new String(b);
System.out.println( str );
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Connect.connect();
tRecv.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.txp.test01;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestReceive { // 接收方
public static void main(String[] args) throws IOException {
System.out.println("咨询老师上线...");
/* 创建套接字 并且指定端口号 */
DatagramSocket ds = new DatagramSocket(9999);
/* 接收数据 */
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
/* 处理数据 */
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println("学生对我说:" + str);
/* 咨询老师做回复 */
/* 发送数据 */
String str2 = "我在呢";
byte[] b2 = str2.getBytes();
DatagramPacket dp2 = new DatagramPacket(b2, b2.length, InetAddress.getByName("192.168.216.1"), 8888);
/* 发送 */
ds.send(dp2);
/* 关闭 */
ds.close();
}
}
package com.txp.test01;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestSend { // 发送方
public static void main(String[] args) throws IOException {
System.out.println("学生上线...");
/* 创建套接字 8888是发送方的接口 */
DatagramSocket ds = new DatagramSocket(8888);
/* 发送数据 */
String str = "你好, 在吗?";
byte[] b = str.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("192.168.216.1"), 9999);
/* 发送 */
ds.send(dp);
/* 咨询老师的回复 */
/* 接收数据 */
byte[] b2 = new byte[1024];
DatagramPacket dp2 = new DatagramPacket(b2, b2.length);
ds.receive(dp2);
/* 处理数据 */
String str2 = new String(dp2.getData(), 0, dp2.getLength());
System.out.println("老师对我说:" + str2);
/* 关闭 */
ds.close();
}
}
package com.txp.test02;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class TestReceive { // 接收方
public static void main(String[] args) {
System.out.println("咨询老师上线...");
DatagramSocket ds = null;
try {
ds = new DatagramSocket(9999);
while(true) {
/* 接收数据 */
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
/* 处理数据 */
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println("学生说:" + str);
if("拜拜".equals(str)) {
System.out.println("老师结束对话");
break;
}
/* 咨询老师做回复 */
/* 发送数据 */
Scanner sc = new Scanner(System.in);
System.out.print("老师:");
String str2 = sc.next();
byte[] b2 = str2.getBytes();
DatagramPacket dp2 = new DatagramPacket(b2, b2.length, InetAddress.getByName("192.168.216.1"), 8888);
/* 发送 */
ds.send(dp2);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 关闭 */
ds.close();
}
}
}
package com.txp.test02;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class TestSend { // 发送方
public static void main(String[] args) {
System.out.println("学生上线...");
/* 创建套接字 8888是发送方的接口 */
DatagramSocket ds = null;
try {
ds = new DatagramSocket(8888);
while(true) {
/* 发送数据 */
Scanner sc = new Scanner(System.in);
System.out.print("学生:");
String str = sc.next();
byte[] b = str.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("192.168.216.1"), 9999);
/* 发送 */
ds.send(dp);
/* 学生结束对话 */
if("拜拜".equals(str)) {
System.out.println("学生结束了对话...");
break;
}
/* 咨询老师的回复 */
/* 接收数据 */
byte[] b2 = new byte[1024];
DatagramPacket dp2 = new DatagramPacket(b2, b2.length);
ds.receive(dp2);
/* 处理数据 */
String str2 = new String(dp2.getData(), 0, dp2.getLength());
System.out.println("老师说:" + str2);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 关闭 */
ds.close();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment