小型聊天 Socket 如此聊天一个客户端 一个服务端 怎么通过流来处理 使其可以正常进行聊天 一句一句的 不需要考虑线程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不就是把输入的字写在socket.getOutputStream(),把对方写过来的字从socket.getInputStream()读出来并显示么 这个简单的代码你可以看一下:import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ServerTest{ServerSocket ss = null;Socket s =null;DataInputStream c = null;String str = null;public void Lunch(){try{ss = new ServerSocket(5656);}catch (IOException e1){e1.printStackTrace();}try {this.s = ss.accept(); //这里有SocketException异常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 ServerTest().Lunch();}}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 ClientTest {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 ClientTest().Lunch();}} 一道调度问题 SCJP线程题目 求教 如何求堆栈的长度? 求教有关filedialog的问题? JAVA 数据库问题,高手救我,已经崩溃了。。。 关于Socket设置连接超时 这个程序应该无问题,但运行出错,请帮忙看下 如何修改Swing默认风格? 关于Vector 的同步问题。 为什么我通过jdbc-odbc桥连接到access数据库的时候resultset只能向后移不能前移或作其它移动??? 有人用过zeroMQ吗?一直提示C:\Program Files\Java\jre6\bin\jzmq.dll: Can't find dependent li 事件监听
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest
{
ServerSocket ss = null;
Socket s =null;
DataInputStream c = null;
String str = null;
public void Lunch()
{
try
{
ss = new ServerSocket(5656);
}
catch (IOException e1)
{
e1.printStackTrace();
}
try
{
this.s = ss.accept(); //这里有SocketException异常
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 ServerTest().Lunch();
}
}
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 ClientTest {
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 ClientTest().Lunch();
}
}