|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么在Windows上能运行,在Linux虚拟机上就运行不了啊?代码:
Client
import org.json.simple.JSONObject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private static Socket socket;
public static boolean connection_state = false;
public static void main(String[] args){
while (!connection_state) {
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
private static void connect(){
try {
socket = new Socket("127.0.0.1", 9999);
connection_state = true;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
new Thread(new Client_listen(socket,ois)).start();
new Thread(new Client_send(socket,oos)).start();
new Thread(new Client_heart(socket,oos)).start();
}catch (Exception e){
e.printStackTrace();
connection_state = false;
}
}
public static void reconnect(){
while (!connection_state){
System.out.println("正在尝试重新链接.....");
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
class Client_listen implements Runnable{
private Socket socket;
private ObjectInputStream ois;
Client_listen(Socket socket,ObjectInputStream ois){
this.socket = socket;
this.ois = ois;
}
@Override
public void run() {
try {
while (true){
System.out.println(ois.readObject());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Client_send implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_send(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入你要发送的信息:");
String string = scanner.nextLine();
JSONObject object = new JSONObject();
object.put("type","chat");
object.put("msg",string);
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
class Client_heart implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_heart(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
System.out.println("心跳包线程已启动...");
while (true){
Thread.sleep(5000);
JSONObject object = new JSONObject();
object.put("type","heart");
object.put("msg","心跳包");
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
Server:
import org.json.simple.JSONObject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args){
try {
System.out.println("Socket服务器开始运行...");
ServerSocket serverSocket = new ServerSocket(9999);
while (true){
Socket socket = serverSocket.accept();
new Thread(new Server_listen(socket)).start();
new Thread(new Server_send(socket)).start();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Server_listen implements Runnable{
private Socket socket;
Server_listen(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
while (true)
System.out.println(ois.readObject());
}catch (Exception e){
e.printStackTrace();
}finally {
try{
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
class Server_send implements Runnable{
private Socket socket;
Server_send(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入要发送的内容:");
String string = scanner.nextLine();
JSONObject object = new JSONObject();
object.put("type","chat");
object.put("msg",string);
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
(代码是网上找的,网址是https://gitee.com/xiangfangsong/Java-socket)
|
|