server端
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
DataInputStream c = null;
String str = null;
public void Lunch(){
ServerSocket ss = null;
Socket s =null;
try {
ss = new ServerSocket(5656);
} catch (IOException e1) {
e1.printStackTrace();
}try {
while(true){
s = ss.accept();
System.out.println("新的客户端已经连接");
Client a = new Client(s);
new Thread(a).start();
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}}
class Client implements Runnable{
private Socket s;
Client(Socket s){
this.s = s;
try {
c = new DataInputStream(s.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(true){
str = c.readUTF();
System.out.println(str);}
}
catch (IOException e) {
System.out.println("已断开连接");
}
finally{
try {
if(c != null) c.close();
if(s !=null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
public static void main(String[] args) {
new Server().Lunch();}}client端
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
import java.io.*;public class Client {
DataOutputStream c = null;
TextField t1 = new TextField();
TextArea t2 = new TextArea();
Frame a = new Frame();
String str = null;
Socket s = null;
TextLister tt = new TextLister();
public void add(){
a.add(t1 , "South");
a.add(t2 , "North");
a.pack();
}
public void close(){
a.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try {
s.close();
c.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
}
public void addText(){
t1.addActionListener(tt);
}
public void Lunch(){
this.add();
this.close();
this.addText();
a.setVisible(true);
}
class TextLister implements ActionListener{public void actionPerformed(ActionEvent e) {
try {
s = new Socket("127.0.0.1" , 5656);
c = new DataOutputStream(s.getOutputStream());
} catch (IOException e1) {
System.out.println("找不到服务器");
}
str = t1.getText();
t2.setText(str);
try {
c.writeUTF(str);
c.flush();
} catch (IOException e1) {
System.out.println("服务器无法接受");
}
t1.setText("");
}
}
public static void main(String[] args) {
new Client().Lunch();
}
}
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
DataInputStream c = null;
String str = null;
public void Lunch(){
ServerSocket ss = null;
Socket s =null;
try {
ss = new ServerSocket(5656);
} catch (IOException e1) {
e1.printStackTrace();
}try {
while(true){
s = ss.accept();
System.out.println("新的客户端已经连接");
Client a = new Client(s);
new Thread(a).start();
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}}
class Client implements Runnable{
private Socket s;
Client(Socket s){
this.s = s;
try {
c = new DataInputStream(s.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(true){
str = c.readUTF();
System.out.println(str);}
}
catch (IOException e) {
System.out.println("已断开连接");
}
finally{
try {
if(c != null) c.close();
if(s !=null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
public static void main(String[] args) {
new Server().Lunch();}}client端
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
import java.io.*;public class Client {
DataOutputStream c = null;
TextField t1 = new TextField();
TextArea t2 = new TextArea();
Frame a = new Frame();
String str = null;
Socket s = null;
TextLister tt = new TextLister();
public void add(){
a.add(t1 , "South");
a.add(t2 , "North");
a.pack();
}
public void close(){
a.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try {
s.close();
c.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
}
public void addText(){
t1.addActionListener(tt);
}
public void Lunch(){
this.add();
this.close();
this.addText();
a.setVisible(true);
}
class TextLister implements ActionListener{public void actionPerformed(ActionEvent e) {
try {
s = new Socket("127.0.0.1" , 5656);
c = new DataOutputStream(s.getOutputStream());
} catch (IOException e1) {
System.out.println("找不到服务器");
}
str = t1.getText();
t2.setText(str);
try {
c.writeUTF(str);
c.flush();
} catch (IOException e1) {
System.out.println("服务器无法接受");
}
t1.setText("");
}
}
public static void main(String[] args) {
new Client().Lunch();
}
}
解决方案 »
- 如何去除数组中的空值?
- 事务问题关于 JDBC 的会的赶紧进吧
- URL读取远程图片!
- (在线)querydataset打开失败!
- [100分]日期从2004-01-01到2005-01-18(今天),如何作个循环把这之间的每个日期都显示出来?急
- 在线等怎么把Char c='9',转换成整型数9.为什么我import java.math.*,还是不能识别round这样的函数?
- Java用des加密后用 linux c语言解密 怎么互通?
- 求救!!!!java 怎样不擦除直线!!!!!!
- 欢迎讨论inner classes
- 关于thinking in java
- 我做了一个单机版小游戏,就是不知道怎么改成网络版的,请教一个问题
- 我想将一个list里的数据飞开,每20条为一个单元,应该怎么做
运行结果为什么会不同。