服务端:
import java.io.IOException;
import java.net.*;
public class ServerComputer {
public static final int PORT = 8888;
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(PORT);
System.out.println("服务器启动----》等待连接");
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"上线了!!!!");
} catch (IOException e) {
System.out.println("ss = new ServerSocket(PORT)异常");
}finally {
if(ss != null) {
try {
ss.close();
} catch (IOException e) {
System.out.println("ss.close异常");
}
}
}
}
}
客户端:
package com.jsh.SelfTcpTest2;
import java.io.IOException;
import java.net.*;
public class ClientComputer {
public static final int PORT = 8888;
public static final String IP = "192.168.121.1";
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket(IP ,PORT);
System.out.println("连接成功!!!!");
} catch (UnknownHostException e) {
System.out.println("ip异常");
} catch (IOException e) {
System.out.println("s = new Socket 异常");
}finally {
if(s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------
简单的实现多个客户端连接 服务端
服务:
import java.io.IOException;
import java.net.;
import java.io.;
public class ServerComputer {
public static final int PORT = 8888;
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(PORT);
System.out.println("服务器启动----》等待连接");
while(true) {
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"上线了!!!!");
PrintWriter pw = new PrintWriter(s.getOutputStream() , true);
pw.println("欢迎来到****服务器!!!");
}
} catch (IOException e) {
System.out.println("ss = new ServerSocket(PORT)异常");
}finally {
if(ss != null) {
try {
ss.close();
} catch (IOException e) {
System.out.println("ss.close异常");
}
}
}
}
}
客户:
import java.io.;
import java.net.;
public class ClientComputer {
public static final int PORT = 8888;
public static final String IP = “192.168.121.1”;
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket(IP ,PORT);
System.out.println("连接成功!!!!");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = null;
while((str = br.readLine()) != null) {
System.out.println("服务器发来消息"+str);
}
} catch (UnknownHostException e) {
System.out.println("ip异常");
} catch (IOException e) {
System.out.println("s = new Socket 异常");
}finally {
if(s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.实现 服务器 子线程接受 客户端的消息 (为了解决 服务器一致在接受循环里等待 ,其他客户端不能正常连接 服务端)
2.为了解决 两种异常
一、异常种类
1.服务端未启动
2.客户端异常退出 导致服务器也 奔溃了
完善版本:
客户端:
import java.io.;
import java.net.;
public class ClientComputer {
public static final int PORT = 8888;
public static final String IP = “192.168.121.1”;
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket(IP ,PORT);
System.out.println("连接成功!!!!");
//BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
Reveice R = new Reveice(s);
new Thread(R).start();
PrintWriter pw = new PrintWriter(s.getOutputStream() ,true);
BufferedReader brr = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.println("请输入需要发送的语句");
String msg = brr.readLine();
pw.println(msg);
}
}catch (ConnectException e) {
System.out.println("服务器未启动!!");
} catch (UnknownHostException e) {
System.out.println("ip异常");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Reveice implements Runnable{
private Socket s;
private BufferedReader br ;
public Reveice() {
}
public Reveice(Socket s) throws IOException {
this.s = s;
this.br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
@Override
public void run() {
String str = null;
try {
while((str = this.br.readLine()) != null) {
System.out.println("服务器发来消息"+str);
}
} catch (SocketException e) {
System.out.println("服务器掉线了");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(this.s != null) {
try {
this.s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端:
import java.net.;
import java.io.;
public class ServerComputer {
public static final int PORT = 8888;
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(PORT);
System.out.println("服务器启动----》等待连接");
while(true) {
Socket s = ss.accept();
Rthread r = new Rthread(s);
new Thread(r).start();
}
} catch (IOException e) {
System.out.println("ss = new ServerSocket(PORT)异常");
}finally {
if(ss != null) {
try {
ss.close();
} catch (IOException e) {
System.out.println("ss.close异常");
}
}
}
}
}
class Rthread implements Runnable{
private Socket s;
private BufferedReader br ;
private PrintWriter pw;
public Rthread() {
}
public Rthread(Socket s) throws IOException {
this.s = s;
this.br = new BufferedReader(new InputStreamReader(s.getInputStream()));
this.pw = new PrintWriter(s.getOutputStream() , true);
}
@Override
public void run() {
System.out.println(s.getInetAddress().getHostAddress()+"上线了!!!!");
this.pw.println("欢迎来到****服务器!!!");
receive();
}
private void receive() {
String str = null;
try {
while((str = this.br.readLine()) != null) {
if(str.equalsIgnoreCase("exit")) {
this.pw.println("你要下线了");
break;
}
System.out.println("客户端"+s.getInetAddress().getHostName()+s.getPort()+"发来消息"+str);
this.pw.println(s.getInetAddress().getHostName()+s.getPort()+str);
}
System.out.println(s.getInetAddress().getHostName()+s.getPort()+"正常退出!!");
}catch (SocketException e) {
System.out.println(s.getInetAddress().getHostName()+s.getPort()+"异常退出!!");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(this.s != null) {
try {
this.s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}