本人是java的菜鸟中的菜鸟,现有一个问题实在是不懂,以下是本人写的一段聊天程序的代码:
服务器端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
ServerSocket server;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));
String ch;
while((ch=dis.readLine())!=null){
System.out.println(ch);
}
}
}
catch(Exception e){
}
}
public void run(){
try{
OutputStream os=client.getOutputStream();
dos=new DataOutputStream(os);
int ch;
while((ch=System.in.read())!=-1){ if(ch=='\n'){
dos.flush();
}
dos.write(ch);
}
}
catch(Exception e){
}
}
public void output(){
try{
server=new ServerSocket(1234);
client =server.accept();
}
catch(Exception e){}
Thread a=new Thread(this);
a.start();
input();
}
public static void main(String[] args) {
client1 b=new client1();
b.output();
}
}
客户端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));
String ch;
while((ch=dis.readLine())!=null){
System.out.println(ch);
}
}
}
catch(Exception e){
}
}
public void run(){
try{
OutputStream os=client.getOutputStream();
dos=new DataOutputStream(os);
int ch;
while((ch=System.in.read())!=-1){
if(ch=='\n'){
dos.flush();
}
dos.write(ch);
}
}
catch(Exception e){
}
}
public void output(){
try{
client =new Socket("localhost",1234);
}
catch(Exception e){}
Thread a=new Thread(this);
a.start();
input();
}
public static void main(String[] args) {
client1 b=new client1();
b.output();
}
}
但是运行时如果连续输入的话只会输出第一次输入的东西,请各位高手帮个忙告诉我这是为什么啊?
服务器端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
ServerSocket server;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));
String ch;
while((ch=dis.readLine())!=null){
System.out.println(ch);
}
}
}
catch(Exception e){
}
}
public void run(){
try{
OutputStream os=client.getOutputStream();
dos=new DataOutputStream(os);
int ch;
while((ch=System.in.read())!=-1){ if(ch=='\n'){
dos.flush();
}
dos.write(ch);
}
}
catch(Exception e){
}
}
public void output(){
try{
server=new ServerSocket(1234);
client =server.accept();
}
catch(Exception e){}
Thread a=new Thread(this);
a.start();
input();
}
public static void main(String[] args) {
client1 b=new client1();
b.output();
}
}
客户端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));
String ch;
while((ch=dis.readLine())!=null){
System.out.println(ch);
}
}
}
catch(Exception e){
}
}
public void run(){
try{
OutputStream os=client.getOutputStream();
dos=new DataOutputStream(os);
int ch;
while((ch=System.in.read())!=-1){
if(ch=='\n'){
dos.flush();
}
dos.write(ch);
}
}
catch(Exception e){
}
}
public void output(){
try{
client =new Socket("localhost",1234);
}
catch(Exception e){}
Thread a=new Thread(this);
a.start();
input();
}
public static void main(String[] args) {
client1 b=new client1();
b.output();
}
}
但是运行时如果连续输入的话只会输出第一次输入的东西,请各位高手帮个忙告诉我这是为什么啊?
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
try
{
ServerSocket ss = new ServerSocket(8001);
System.out.println("server start");
boolean bRunning = true;
while(bRunning)
{
Socket s = ss.accept();
new Thread(new Servicer(s)).start();
}
ss.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}import java.net.*;
import java.io.*;public class Servicer implements Runnable {
/**
* Method run
*
*
*/
private Socket s = null;
public Servicer(Socket s)
{
this.s = s;
}
public void run()
{
try
{
InputStream ips = s.getInputStream();
OutputStream ops = s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
PrintWriter pw = new PrintWriter(ops,true);
while(true)
{
String strline = br.readLine();
if(strline.equalsIgnoreCase("quit"))
{
break;
}
System.out.println(strline);
String strecho = new StringBuffer(strline).reverse().toString();
pw.println(strline+strecho);
}
pw.close();
br.close();
ops.close();
ips.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
服务器端
import java.io.*;
import java.net.*;public class TalkServer{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(4700);
//创建一个ServerSocket在端口4700监听客户请求
}catch(Exception e) {
System.out.println("can not listen to:"+e);
//出错,打印出错信息
} Socket socket=null;
try{
socket=server.accept();
//使用accept()阻塞等待客户请求,有客
//请求到来则产生一个Socket对象,并继续执行
}catch(Exception e) {
System.out.println("Error."+e);
//出错,打印出错信息
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));//由Socket对象得到输入流,并构造相应的BufferedReader对象 PrintWriter os=newPrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
System.out.println("Client:"+is.readLine());
//在标准输出上打印从客户端读入的字符串
line=sin.readLine();
//从标准输入读入一字符串
while(!line.equals("bye")){
//如果该字符串为 "bye",则停止循环
os.println(line); os.flush();
//刷新输出流,使Client马上收到该字符串
System.out.println("Server:"+line);
//在系统标准输出上打印读入的字符串
System.out.println("Client:"+is.readLine()); line=sin.readLine();
//从系统标准输入读入一字符串
}
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
server.close(); //关闭ServerSocket
}catch(Exception e){
System.out.println("Error:"+e);
//出错,打印出错信息
}
}
}
客户端
import java.io.*;
import java.net.*;
public class TalkClient {
public static void main(String args[]){
try{
Socket socket=new Socket("127.0.0.1",4700);
//向本机的4700端口发出客户请
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
PrintWriter os=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
String readline;
readline=sin.readLine(); //从系统标准输入读入一字符串
while(!readline.equals("bye")){
//若从标准输入读入的字符串为 "bye"则停止循环
os.println(readline);
//将从系统标准输入读入的字符串输出到Server
os.flush();
//刷新输出流,使Server马上收到该字
System.out.println("Client:"+readline);
//在系统标准输出上打印读入的字符串
System.out.println("Server:"+is.readLine());
readline=sin.readLine(); //从系统标准输入读入一字符串
}//继续循环
os.close(); //关闭Socket输出流
is.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
System.out.println("Error"+e); //出错,则打印出错信息
}
}
}