当前位置: 技术问答>java相关
在线等待,去掉一个错误给10分!急死我了
来源: 互联网 发布时间:2015-04-07
本文导语: /**author 中国 南阳理工学院 梁肖碧 liangxiaobi@hotmail.com *http://outdo.cnnb.net outdo 空间 *j2sdk1.30 Editplus2.10c.. */ /** *MyServer class **/ import java.io.*; import java.net.*; public class MyServer{ String hostIp; final int liste...
/**author 中国 南阳理工学院 梁肖碧 liangxiaobi@hotmail.com
*http://outdo.cnnb.net outdo 空间
*j2sdk1.30 Editplus2.10c..
*/
/**
*MyServer class
**/
import java.io.*;
import java.net.*;
public class MyServer{
String hostIp;
final int listenPort=8000;
//listening for connection.
public void listen(){
try{
ServerSocket server=new ServerSocket(listenPort);
Socket incoming=null;
while(true){
incoming = server.accept();
connectionhandler(incoming);
}
}catch(BindException be){
System.out.println("Unable to bind port "+listenPort);
}catch(IOException ioe){
System.out.println("Unable to instantiate a ServerSocket on port "+listenPort);
}
}
}
//handler listening..
public void connectionhandler(Socket incoming){
try{
InputStream inFromSocket = incoming.getInputStream();
OutputStream outToSocket= incoming.getOutpuStream();
BufferedReader readString = new BufferedReader(new InputStreamReader(inFromSocket));
System.out.println("received String From client "+readString.readline());
PrintWriter stringWriter=new PrintWriter(incoming.getOutputStream());
stringWriter = readString.readline();
}catch(IOException ioe){
System.out.println(ioe);
}
}
public void close(){
try{
readString.close();
stringWriter.close();
}catch(IOException e){
System.out.println("ServerSocket not closed");
}
}
public static void main(String[] args){
MyServer ms = new MyServer();
ms.listen();
ms.close();
}
}
*http://outdo.cnnb.net outdo 空间
*j2sdk1.30 Editplus2.10c..
*/
/**
*MyServer class
**/
import java.io.*;
import java.net.*;
public class MyServer{
String hostIp;
final int listenPort=8000;
//listening for connection.
public void listen(){
try{
ServerSocket server=new ServerSocket(listenPort);
Socket incoming=null;
while(true){
incoming = server.accept();
connectionhandler(incoming);
}
}catch(BindException be){
System.out.println("Unable to bind port "+listenPort);
}catch(IOException ioe){
System.out.println("Unable to instantiate a ServerSocket on port "+listenPort);
}
}
}
//handler listening..
public void connectionhandler(Socket incoming){
try{
InputStream inFromSocket = incoming.getInputStream();
OutputStream outToSocket= incoming.getOutpuStream();
BufferedReader readString = new BufferedReader(new InputStreamReader(inFromSocket));
System.out.println("received String From client "+readString.readline());
PrintWriter stringWriter=new PrintWriter(incoming.getOutputStream());
stringWriter = readString.readline();
}catch(IOException ioe){
System.out.println(ioe);
}
}
public void close(){
try{
readString.close();
stringWriter.close();
}catch(IOException e){
System.out.println("ServerSocket not closed");
}
}
public static void main(String[] args){
MyServer ms = new MyServer();
ms.listen();
ms.close();
}
}
|
你多了一个}:
public void listen(){
try{
ServerSocket server=new ServerSocket(listenPort);
Socket incoming=null;
while(true){
incoming = server.accept();
connectionhandler(incoming);
}
}catch(BindException be){
System.out.println("Unable to bind port "+listenPort);
}catch(IOException ioe){
System.out.println("Unable to instantiate a ServerSocket on port "+listenPort);
}
}//多的}
}
public void listen(){
try{
ServerSocket server=new ServerSocket(listenPort);
Socket incoming=null;
while(true){
incoming = server.accept();
connectionhandler(incoming);
}
}catch(BindException be){
System.out.println("Unable to bind port "+listenPort);
}catch(IOException ioe){
System.out.println("Unable to instantiate a ServerSocket on port "+listenPort);
}
}//多的}
}
|
OutputStream outToSocket= incoming.getOutpuStream();
>>>>>
OutputStream outToSocket= incoming.getOutputStream();
>>>>>
OutputStream outToSocket= incoming.getOutputStream();
|
好像不是brainy说的问题。
你的程序很怪,你是每接到一个连接,只发一个信息给对方,只从对方接收一次信息??
你的程序很怪,你是每接到一个连接,只发一个信息给对方,只从对方接收一次信息??
|
不过程序的结构不是很理想!