分享一下我写的代码,自豪地分享,完全原创。
以下为服务器端的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
public class Server {
public static void main(String[] args) {
CreateServer cs = new CreateServer() ;
SReserive sr = new SReserive(cs.getSocket()) ;
sr.start() ;
ServerSend ss = new ServerSend(cs.getSocket()) ;
ss.start() ;
}
}
class CreateServer {
private ServerSocket server = null ;
private Socket socket = null ;
public CreateServer() {
try {
server = new ServerSocket(5000) ;
socket = server.accept() ;
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
public ServerSocket getServer() {
return server;
}
public void setServer(ServerSocket server) {
this.server = server;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
class SReserive extends Thread {
private Socket socket ;
private BufferedReader br ;
public SReserive(Socket socket) {
this.socket = socket ;
try {
br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())) ;
}catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String line ;
line = br.readLine() ;
while(!line.equals("bye")) {
System.out.println("Client: " + line) ;
line = br.readLine() ;
}
}catch(IOException e) {
e.printStackTrace() ;
}
}
}
class ServerSend extends Thread {
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
private PrintStream ps ;
public ServerSend(Socket socket) {
try {
ps = new PrintStream(socket.getOutputStream()) ;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String line ;
try {
line = br.readLine() ;
while(!line.equals("bye")) {
ps.println(line) ;
ps.flush() ;
line = br.readLine() ;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Close {
public Close(Socket socket, ServerSocket server) {
try {
socket.close() ;
server.close() ;
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
一下是客户端代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
CreateConnection cc = new CreateConnection() ;
Sending sd = new Sending(cc.getSocket()) ;
sd.start() ;
CReserive cr = new CReserive(cc.getSocket()) ;
cr.start() ;
}
}
class CreateConnection {
private Socket socket ;
private InetAddress ia ;
public CreateConnection() {
try {
ia = InetAddress.getByAddress(new byte[]{(byte)127,(byte)0,(byte)1,(byte)1}) ;
socket = new Socket(ia, 5000) ;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
class Sending extends Thread {
private Socket socket ;
private BufferedReader br ;
private PrintStream ps ;
public Sending(Socket socket) {
this.socket = socket ;
br = new BufferedReader(new InputStreamReader(System.in)) ;
try {
ps = new PrintStream(this.socket.getOutputStream()) ;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String line ;
try {
line = br.readLine() ;
while(!line.equals("bye")) {
ps.println(line) ;
ps.flush() ;
line = br.readLine() ;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class CReserive extends Thread {
private Socket socket ;
private BufferedReader br ;
public CReserive(Socket socket) {
this.socket = socket ;
try {
br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())) ;
}catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String line ;
line = br.readLine() ;
while(!line.equals("bye")) {
System.out.println("Server: " + line) ;
line = br.readLine() ;
}
}catch(IOException e) {
e.printStackTrace() ;
}
}
}
解析一下两端代码的功能,服务器端和客户端同为多线程实现,同为两个线程,一个接受数据并打印,一个发送数据,读者可以自己理解下,不明白的可以给我留言!
文章来自 你让梦想有了翅膀,转载请注明出处
java本地客户端与服务器连接并相互发送数据
原创
©著作权归作者所有:来自51CTO博客作者你爱大白菜的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:java连接mysql
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章