SOC可以收到报文,但是调试的时候发现
DataOutputStream outToClient=new DataOutputStream(soc.getOutputStream());
这里出现了 请问高手什么原因啊。。
因为偶是穷人,所以没有什么分数,请大家原谅啊
高手救命啊。。
package bank;import java.io.*;
import java.net.*;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MainServer{
static int port;
static Thread thread;
static DataInputStream dataInputStream;
static DataOutputStream dataOutStream;
static ServerSocket s=null;
//String
public static void main (String[] args)throws IOException {
System.out.println("服务器启动.......");
BufferedReader fileIn=null;
try{
fileIn = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
String line;
fileIn.close();
}catch(IOException e){
System.out.println("请确保在config\\下面存在着bank.txt文件");
System.exit(1);
}
port = 1078;
s = new ServerSocket(port);
while(true){
try{
Socket soc = s.accept();
BufferedReader inFormClient = new BufferedReader(new InputStreamReader(soc.getInputStream()));
String msg;
char[] a = new char[1000];
int recvLen = inFormClient.read(a, 0, 1000);
if(recvLen < 0) {
System.out.println("收报文失败");
}else{
for(int i = 0 ; i < recvLen ; i ++){
System.out.print(a[i] + ",");
}
System.out.print("\n");
}
inFormClient.close();
inFormClient = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
char[] charmsg = new char[10000];
for(int i = 0 ; i < 10000 ; i ++){
charmsg[i] = '\0';
}
//inFormClient.
int sendLen = inFormClient.read(charmsg, 0 , 10000);
if(sendLen < 0) {
System.out.println("读config\\bank.txt文件失败");
}
msg = new String(charmsg, 0, sendLen);
try{
System.out.println("发送文件MSG :" + msg);
Thread.sleep(1000);
DataOutputStream outToClient=new DataOutputStream(soc.getOutputStream());
//System.out.println("msg")
outToClient.writeChars(msg);
soc.close();
outToClient.flush();
}catch(IOException ex){
System.out.println("写文件失败!");
soc.close();
}
inFormClient.close();
}catch(Exception e){
System.out.println("启动SOCKET出错!");
} } }}
DataOutputStream outToClient=new DataOutputStream(soc.getOutputStream());
这里出现了 请问高手什么原因啊。。
因为偶是穷人,所以没有什么分数,请大家原谅啊
高手救命啊。。
package bank;import java.io.*;
import java.net.*;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MainServer{
static int port;
static Thread thread;
static DataInputStream dataInputStream;
static DataOutputStream dataOutStream;
static ServerSocket s=null;
//String
public static void main (String[] args)throws IOException {
System.out.println("服务器启动.......");
BufferedReader fileIn=null;
try{
fileIn = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
String line;
fileIn.close();
}catch(IOException e){
System.out.println("请确保在config\\下面存在着bank.txt文件");
System.exit(1);
}
port = 1078;
s = new ServerSocket(port);
while(true){
try{
Socket soc = s.accept();
BufferedReader inFormClient = new BufferedReader(new InputStreamReader(soc.getInputStream()));
String msg;
char[] a = new char[1000];
int recvLen = inFormClient.read(a, 0, 1000);
if(recvLen < 0) {
System.out.println("收报文失败");
}else{
for(int i = 0 ; i < recvLen ; i ++){
System.out.print(a[i] + ",");
}
System.out.print("\n");
}
inFormClient.close();
inFormClient = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
char[] charmsg = new char[10000];
for(int i = 0 ; i < 10000 ; i ++){
charmsg[i] = '\0';
}
//inFormClient.
int sendLen = inFormClient.read(charmsg, 0 , 10000);
if(sendLen < 0) {
System.out.println("读config\\bank.txt文件失败");
}
msg = new String(charmsg, 0, sendLen);
try{
System.out.println("发送文件MSG :" + msg);
Thread.sleep(1000);
DataOutputStream outToClient=new DataOutputStream(soc.getOutputStream());
//System.out.println("msg")
outToClient.writeChars(msg);
soc.close();
outToClient.flush();
}catch(IOException ex){
System.out.println("写文件失败!");
soc.close();
}
inFormClient.close();
}catch(Exception e){
System.out.println("启动SOCKET出错!");
} } }}
static int port;
static Thread thread;
static DataInputStream dataInputStream;
static DataOutputStream dataOutStream;
static ServerSocket s=null;
//String
public static void main (String[] args)throws IOException {
System.out.println("服务器启动.......");
BufferedReader fileIn=null;
try{
fileIn = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
String line;
fileIn.close();
}catch(IOException e){
System.out.println("请确保在config\\下面存在着bank.txt文件");
System.exit(1);
}
port = 1078;
s = new ServerSocket(port);
while(true){
try{
Socket soc = s.accept();
BufferedReader inFormClient = new BufferedReader(new InputStreamReader(soc.getInputStream()));
String msg;
char[] a = new char[1000];
int recvLen = inFormClient.read(a, 0, 1000);
if(recvLen < 0) {
System.out.println("收报文失败");
}else{
for(int i = 0 ; i < recvLen ; i ++){
System.out.print(a[i] + ",");
}
System.out.print("\n");
}
// inFormClient.close();//这一行注释掉,在finally块关,这句导致socket关闭
inFormClient = new BufferedReader(new FileReader("c:\\config\\bank.txt"));
char[] charmsg = new char[10000];
for(int i = 0 ; i < 10000 ; i ++){
charmsg[i] = '\0';
}
//inFormClient.
int sendLen = inFormClient.read(charmsg, 0 , 10000);
if(sendLen < 0) {
System.out.println("读config\\bank.txt文件失败");
}
msg = new String(charmsg, 0, sendLen);
try{
System.out.println("发送文件MSG :" + msg);
Thread.sleep(1000);
DataOutputStream outToClient=new DataOutputStream(soc.getOutputStream());
//System.out.println("msg")
outToClient.writeChars(msg);
outToClient.flush();
}catch(IOException ex){
System.out.println("写文件失败!");
}finally{
try{ soc.close();
}catch(Exception e){}
}
inFormClient.close();
}catch(Exception e){
System.out.println("启动SOCKET出错!");
}
finally{
//TODO 关闭inFormClient,outToClient
} } }}