Commit e43fbe91 authored by txp's avatar txp

合并代码

parent e3bbf3f0
...@@ -11,7 +11,7 @@ import java.net.Socket; ...@@ -11,7 +11,7 @@ import java.net.Socket;
public class Server { // 服务器 public class Server { // 服务器
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println("服务器启动..."); System.out.println("服务器启动...");
/* 利用套接字, 指定服务器的端口号 */ /* 利用套接字, 指定服务器的端口号 */
ServerSocket ss = new ServerSocket(8888); ServerSocket ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */ /* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
......
...@@ -15,37 +15,36 @@ public class Client { // ...@@ -15,37 +15,36 @@ public class Client { //
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("客户端启动..."); System.out.println("客户端启动...");
/* 创建套接字 */ /* 创建套接字 */
Socket s = null; Socket socket = null;
Scanner sc = null; Scanner scanner = null;
String name = null; OutputStream outputStream = null;
OutputStream os = null; ObjectOutputStream objectOutputStream = null;
ObjectOutputStream oos = null; InputStream inputStream = null;
InputStream is = null; DataInputStream dataInputStream = null;
DataInputStream dis = null;
try { try {
s = new Socket(InetAddress.getByName("192.168.3.3"), 8888); socket = new Socket(InetAddress.getByName("192.168.3.3"), 8888);
/* 1.键盘录入账号和密码 */ /* 1.键盘录入账号和密码 */
sc = new Scanner(System.in); scanner = new Scanner(System.in);
System.out.println("账号:"); System.out.println("账号:");
name = sc.next(); String name = scanner.next();
System.out.println("密码:"); System.out.println("密码:");
String password = sc.next(); String password = scanner.next();
/* 2.将上面的账号密码封装成一个对象 */ /* 2.将上面的账号密码封装成一个对象 */
User user = new User(name, password); User user = new User(name, password);
/* 3.将这个对象发送给服务器端 /* 3.将这个对象发送给服务器端
* 我们感受到的应用层向外传送数据是:输出流 */ * 我们感受到的应用层向外传送数据是:输出流 */
os = s.getOutputStream(); outputStream = socket.getOutputStream();
oos = new ObjectOutputStream(os); objectOutputStream = new ObjectOutputStream(outputStream);
oos.writeObject(user); objectOutputStream.writeObject(user);
/* 客户端要接收服务器的反馈信息 */ /* 客户端要接收服务器的反馈信息 */
s.shutdownOutput(); socket.shutdownOutput();
is = s.getInputStream(); inputStream = socket.getInputStream();
dis = new DataInputStream(is); dataInputStream = new DataInputStream(inputStream);
boolean flag = dis.readBoolean(); boolean flag = dataInputStream.readBoolean();
if(flag) { if(flag) {
System.out.println("登陆成功!"); System.out.println("登陆成功!");
} else { } else {
...@@ -58,32 +57,32 @@ public class Client { // ...@@ -58,32 +57,32 @@ public class Client { //
} finally { } finally {
/* 关闭流, 关闭Socket资源 */ /* 关闭流, 关闭Socket资源 */
try { try {
dis.close(); dataInputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
is.close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
oos.close(); objectOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
os.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
sc.close(); scanner.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
s.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package com.txp.SocketTest01; package com.txp.SocketTest01;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client { public class Client {
public void sendMsg(int i){
public static Socket socket ;
public InetAddress ia;
public static void main(String[] args) throws UnknownHostException {
int i = 0;
while(true){
i ++ ;
sendMsg(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void sendMsg(int i){
try{ try{
System.out.println("发送第"+i+"次"); getSocket().getOutputStream().write(("发送第"+i+"次").getBytes("gbk"));
MySocketConnection.getSocket().getOutputStream().write(("发送第"+i+"次").getBytes("gbk")); System.out.println("已连接服务器, 心跳第"+i+"次");
}catch(Exception e){ }catch(Exception e){
System.out.println("服务器已断开, 正在重连..."); System.out.println("服务器连接已断开, 正在重连...");
MySocketConnection.socket = null; socket = null;
sendMsg(i); sendMsg(i);
} }
} }
public static Socket getSocket(){
try {
if(socket == null){
return new Socket(InetAddress.getByName("192.168.3.3"),8888);
}else{
return socket;
}
} catch (UnknownHostException e) {
} catch (IOException e) {
}
return null;
} }
}
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(1000);
} 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.3.3"),8888);
}else{
return socket;
}
} catch (UnknownHostException e) {
} catch (IOException e) {
}
return null;
}
}
package com.txp.SocketTest01;
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;
try {
ss = new ServerSocket(8888);
/* 服务器这面, 应是一个接收的动作, accept() 阻塞方法 */
while(true) {
s = ss.accept();
new ServerThread(s).start();
if(null == s.getInetAddress()) {
System.out.println("客户端已断开");
}
System.out.println("客户端已连接, 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.txp.SocketTest01;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
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;
DataInputStream dis = null;
OutputStream os = null;
DataOutputStream dos = null;
@Override
public void run() {
try {
/* 应用层还是用输入的IO流接收数据
* 接收客户端过来的对象 */
is = s.getInputStream();
dis = new DataInputStream(is);
/* 服务器端像客户端反馈结果 */
s.shutdownInput();
os = s.getOutputStream();
dos = new DataOutputStream(os);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("客户端未连接");
e.printStackTrace();
} finally {
/* 关闭流 */
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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