作者a73126 (Jalen)
看板java
标题Re: [问题] 找不出原因的SocketException
时间Mon Nov 22 16:41:36 2010
Server(出问题那行用红色标出了)
import java.net.*;
import java.io.*;
import java.lang.Thread;
public class Server {
public static void main(String args[]) throws java.io.IOException {
while(true){
ServerThread thread = new ServerThread();
thread.start();
}
}
}
class ServerThread extends Thread {
public void run() {
try{
ServerSocket sv_socket = new ServerSocket(12345); //宣告一个ServerSocket使用Port 12346
Socket cl_socket = new Socket(); //宣告一个Socket 建立起和Server的Connection
System.out.println("等待客户端连线.....");
while(true) {
//接收部分
cl_socket = sv_socket.accept(); //ServerSocket接受接收
System.out.println("客户端从:"+cl_socket.getInetAddress()+":"+cl_socket.getPort()+"连线");
InputStream input = cl_socket.getInputStream(); //接收Client传送内容
sv_socket.close();
int port_length = input.read(); //read Client Port length
byte[] port_byte = new
byte[port_length];
for(
int i = 0 ; i < port_byte.length ; i++) {
port_byte[i] = (
byte)input.read(); //read Client Port
}
int client_port = Integer.parseInt(new String(port_byte));
int get_length = input.read(); //read 算式长度
byte[] byte_temp = new
byte[get_length];
System.out.println(client_port);
for(
int i = 0 ; i < byte_temp.length ; i++) {
byte_temp[i] = (
byte)input.read(); //read Client送出之算式
}
String str = new String(byte_temp);
System.out.print(str);
//运算部分
char str_array[] = str.toCharArray();
int i = 0 , ans = 0, num1 = 0, num2;
boolean bool = false;
while(i < str_array.length) { //运算算式
switch(str_array[i]) {
case '+':
{
System.out.print("get");
String num[] = str.split("\\+");
num1 = Integer.parseInt(num[0]);
num2 = Integer.parseInt(num[1]);
ans = num1+num2;
System.out.println("ans:"+ans);
bool = true;
i = str_array.length;
break;
}
//省略其他算式判断
default:
{
i++;
break;
}
}
}
if(bool = true) {
//送出运算结果部分
byte int_byte[];
String int_str;
int_str = String.valueOf(ans);
int_byte = int_str.getBytes();
System.out.println("Client Port:"+client_port);
try{
Socket send_socket = new Socket(cl_socket.getInetAddress(), client_port); //建立socket连接Client
System.out.println("Connected?"+send_socket.isConnected());
OutputStream ops = send_socket.getOutputStream();
ops.write(int_byte); //送出答案
send_socket.close();
}
catch(BindException e){
System.out.println(e.toString());
System.out.println(e.getMessage());
}
catch(ConnectException e){
System.out.println(e.toString());
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.toString());
System.out.println(e.getMessage());
}
bool = false;
}
}
}
catch(Exception e){}
}
}
Client部分
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client
{
public static void main(String arg[]) throws UnknownHostException
{
try
{
Scanner input = new Scanner(System.in); //建立Scanner做为键盘输入
ServerSocket sv_socket = new ServerSocket(0); //宣告一个ServerSocket使用电脑空闲的Port
System.out.print("请输入Server IP:");
String address;
address = input.nextLine();
Socket socket = new Socket(address, 12345); //宣告一个Socket 建立起和Server的Connection
System.out.println(socket.getLocalAddress());
int port = sv_socket.getLocalPort(); //取得ServerSocket所使用的空闲Port
System.out.println("port:"+port);
String str_port = Integer.toString(port); //将数字Port型态转换为String
byte[] port_byte = str_port.getBytes(); //将Port型态转换为Byte
OutputStream ops = socket.getOutputStream(); //new OutputStream
ops.write(port_byte.length); //将Port byte的长度送出
ops.write(port_byte); //输出Port的byte串流
System.out.print("请输入算式:");
String str;
str = input.nextLine();
byte[] byte_temp = str.getBytes(); //将输入字串转成byte
ops.write((
byte)str.length()); //送出算式字串的byte长度
ops.write(byte_temp); //输出算式byte串流
socket = sv_socket.accept(); //ServerSocket接受接收
InputStream input_stream = socket.getInputStream();
byte[] get_byte = new
byte[100];
int get_byte_long = input_stream.read(get_byte); //接收byte串流(Server回覆内容)
byte[] byte_ans = new
byte[get_byte_long];
for(
int i = 0 ; i < get_byte_long ; i++)
{
byte_ans[i] = get_byte[i];
}
str = new String(byte_ans); //将接收的byte转回字串输出
System.out.println("运算结果为:"+str);
}
catch(IOException e)
{
}
}
}
--
★小天使 呜呜呜...坏人请你饶我一命
To 小天使: 饶命可以...但是要...嘿嘿嘿嘿嘿
To 小天使: XDDDDDDDDDDDDD
★小天使 要干嘛><?
To 小天使: 要..........给我P币.....XDDDDDD
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.131.66.40
1F:→ tkcn:这样贴 code 很难看! 这份 code 架构有问题,TCP 也有误解 11/22 16:53
3F:→ a73126:了解~感谢这份文件....这部分算式自己摸索API写的所以.... 11/22 17:25
4F:推 qrtt1:if(a = b) 的型式能过 compiler 吗@@? 11/23 09:26
5F:→ tkcn:boolean 可以过,不过一定是 true 11/23 09:50