java实验小程序~~急急急~
编写一个C/S模式程序,实现“客户端向服务器发出信息:‘你好’,服务器接收到信息后,给出回应:‘你好,我是服务器。’这是java程序设计上机实验的一个小程序~~~不用太复...
编写一个C/S模式程序,实现“客户端向服务器发出信息:‘你好’,服务器接收到信息后,给出回应:‘你好,我是服务器。’这是java程序设计上机实验的一个小程序~~~不用太复杂 能用就成
展开
展开全部
服务器:代码
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class ChatServer {
List<client> clients = new ArrayList<client>();
public static void main(String[] args) {
new ChatServer().started();
}
public void started() {
boolean started = false;
ServerSocket ss = null;
try {
ss = new ServerSocket(8888);
started = true;
}catch(BindException e) {
System.out.println("端口使用中");
System.out.println("请关掉相关程序");
System.exit(0);
}
catch(Exception e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
System.out.println("djfldjfdfj");
client c = new client(s);
new Thread(c).start();
clients.add(c);
}
}catch (EOFException e) {
System.out.println("client close");
}
catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class client implements Runnable {
private Socket s = null;
private boolean bconnect = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bconnect = true;
}
public void sent(String str) {
try {
dos.writeUTF(str);
}catch(SocketException e) {
clients.remove(this);
System.out.println("对方推出了");
}
catch (IOException e) {
}
}
public void run() {
try {
while(bconnect) {
String str;
str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++) {
client c = clients.get(i);
c.sent(str);
}
}
}catch(SocketException e) {
System.out.println("client close");
}catch (IOException e) {
System.out.println("对方推出了,=====");
}finally {
try {
if(dos != null) dos.close();
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端代码:
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
public class ChatClient extends Frame{
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Boolean con = false;
TextField tftxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrome();
}
public void launchFrome() {
this.setLocation(400,300);
this.setSize(300,300);
add(tftxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tftxt.addActionListener(new tfListener());
this.setVisible(true);
connect();
new Thread(new receivemessage(s)).start();
}
public void connect() {
try {
s = new Socket("192.168.1.3",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connect");
con = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class tfListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tftxt.getText().trim();
//taContent.setText(str);
tftxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class receivemessage implements Runnable {
private Socket s = null;
public receivemessage(Socket s) {
this.s = s;
}
public void run() {
try {
while(con) {
String str = dis.readUTF();
System.out.println(str);
taContent.setText(taContent.getText() + str +'\n');
}
} catch(SocketException e) {
System.out.println("close byebye");
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class ChatServer {
List<client> clients = new ArrayList<client>();
public static void main(String[] args) {
new ChatServer().started();
}
public void started() {
boolean started = false;
ServerSocket ss = null;
try {
ss = new ServerSocket(8888);
started = true;
}catch(BindException e) {
System.out.println("端口使用中");
System.out.println("请关掉相关程序");
System.exit(0);
}
catch(Exception e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
System.out.println("djfldjfdfj");
client c = new client(s);
new Thread(c).start();
clients.add(c);
}
}catch (EOFException e) {
System.out.println("client close");
}
catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class client implements Runnable {
private Socket s = null;
private boolean bconnect = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bconnect = true;
}
public void sent(String str) {
try {
dos.writeUTF(str);
}catch(SocketException e) {
clients.remove(this);
System.out.println("对方推出了");
}
catch (IOException e) {
}
}
public void run() {
try {
while(bconnect) {
String str;
str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++) {
client c = clients.get(i);
c.sent(str);
}
}
}catch(SocketException e) {
System.out.println("client close");
}catch (IOException e) {
System.out.println("对方推出了,=====");
}finally {
try {
if(dos != null) dos.close();
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端代码:
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
public class ChatClient extends Frame{
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Boolean con = false;
TextField tftxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrome();
}
public void launchFrome() {
this.setLocation(400,300);
this.setSize(300,300);
add(tftxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tftxt.addActionListener(new tfListener());
this.setVisible(true);
connect();
new Thread(new receivemessage(s)).start();
}
public void connect() {
try {
s = new Socket("192.168.1.3",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connect");
con = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class tfListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tftxt.getText().trim();
//taContent.setText(str);
tftxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class receivemessage implements Runnable {
private Socket s = null;
public receivemessage(Socket s) {
this.s = s;
}
public void run() {
try {
while(con) {
String str = dis.readUTF();
System.out.println(str);
taContent.setText(taContent.getText() + str +'\n');
}
} catch(SocketException e) {
System.out.println("close byebye");
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
展开全部
1.简单服务器端
/*
import java.net.*;
import java.io.*;
*/
ServerSocket server=null;
try {
server=new ServerSocket(2000);
}catch(Exception e){
System.out.println("不能监慧衡听:"+e.toString());
}
Socket socket=null;
try {
socket=server.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter br=new PrintWriter(socket.getOutputStream());
String %%2=br.readLine();
br.println("");
br.flush();
br.close();
out.close();
}
catch(IOException e){
System.out.println("出错:"+e.toString());
}finally{
try {
if(socket!=null){
socket.close();
server.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
2.简前州做单客户端
/*
import java.net.*;
import java.io.*;
*/
Socket socket=null;
try {
socket=new Socket("127.0.0.1",2000);
PrintWriter out=new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("");
out.flush();
String %%5=br.readLine();
%%6
out.close();
br.close();
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
socket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
5.点对点迹轿通信
//import java.io.*;
//import java.net.*;
public class %%6 extends Thread {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket(5000);
} catch (Exception e) {
System.out.println("不能监听:" + e.toString());
}
Socket socket = null;
try {
socket = server.accept();
BufferedReader req = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
Debug.p(req.readLine());
os.println("Server");
os.flush();
os.close();
req.close();
} catch (IOException e) {
System.out.println("出错:" + e.toString());
} finally {
try {
if (socket != null) {
socket.close();
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// TODO Auto-generated method stub
Thread t = new %%6();
t.start();
String strIP = null;
try {
strIP = InetAddress.getLocalHost().getHostAddress().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
Socket socket = null;
try {
socket = new Socket(strIP, 4000);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw.println("Client");
pw.flush();
Debug.p(br.readLine());
pw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
import java.net.*;
import java.io.*;
*/
ServerSocket server=null;
try {
server=new ServerSocket(2000);
}catch(Exception e){
System.out.println("不能监慧衡听:"+e.toString());
}
Socket socket=null;
try {
socket=server.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter br=new PrintWriter(socket.getOutputStream());
String %%2=br.readLine();
br.println("");
br.flush();
br.close();
out.close();
}
catch(IOException e){
System.out.println("出错:"+e.toString());
}finally{
try {
if(socket!=null){
socket.close();
server.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
2.简前州做单客户端
/*
import java.net.*;
import java.io.*;
*/
Socket socket=null;
try {
socket=new Socket("127.0.0.1",2000);
PrintWriter out=new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("");
out.flush();
String %%5=br.readLine();
%%6
out.close();
br.close();
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
socket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
5.点对点迹轿通信
//import java.io.*;
//import java.net.*;
public class %%6 extends Thread {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket(5000);
} catch (Exception e) {
System.out.println("不能监听:" + e.toString());
}
Socket socket = null;
try {
socket = server.accept();
BufferedReader req = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
Debug.p(req.readLine());
os.println("Server");
os.flush();
os.close();
req.close();
} catch (IOException e) {
System.out.println("出错:" + e.toString());
} finally {
try {
if (socket != null) {
socket.close();
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// TODO Auto-generated method stub
Thread t = new %%6();
t.start();
String strIP = null;
try {
strIP = InetAddress.getLocalHost().getHostAddress().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
Socket socket = null;
try {
socket = new Socket(strIP, 4000);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw.println("Client");
pw.flush();
Debug.p(br.readLine());
pw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询