我发觉这样很笨,网络浪费很大,程序也毫无优化可言;能不能象c里一样先把预先要传的byte、short、int等全部打到一个BUFFER里再一次性的write出去?
请一个设计实例,谢谢!!!!!
14 个解决方案
#1
实例没有,但是思路有一个:你为什么不把它们打入一个class中,一次性的传过去呢?
writeObject/readObject即可。
读出来时再造型一下,如果你是用java与java通讯,这种方法没问题,如果是java与c通讯,就不行了。
writeObject/readObject即可。
读出来时再造型一下,如果你是用java与java通讯,这种方法没问题,如果是java与c通讯,就不行了。
#2
严重同意楼上的说法!
实际应用常常把预先要传的数据封装在一个class中,你可以自定义,也可以用cllection,ArrayList之类的,方法很多,然后用writeObject/readObject来处理。一个数据一个数据的write,不是很好。
实际应用常常把预先要传的数据封装在一个class中,你可以自定义,也可以用cllection,ArrayList之类的,方法很多,然后用writeObject/readObject来处理。一个数据一个数据的write,不是很好。
#3
偶就是与c通信呀,还的高低转换,ft,继续等待大虾回复了
#4
晕倒
外面套一个BufferedInputStream就可以提高效率了
外面套一个BufferedInputStream就可以提高效率了
#5
呵呵,套个BufferedOutputStream,不过与c通讯很麻烦的
#6
有什么好晕的,套bufferInputStream我也知道,可我就要“打到一个BUFFER里再一次性的write出去”,怎么做嘛?!
#7
比如我举个土方法,不过肯定有问题,只是我所表达希望达到的就是这个大概意思:
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
真正具体实现方法如何?应该是基础问题了吧,可惜我基础不好
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
真正具体实现方法如何?应该是基础问题了吧,可惜我基础不好
#8
比如我举个土方法,不过肯定有问题,只是我所表达希望达到的就是这个大概意思:
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
#9
你的应用层协议设计有问题,必然导致这个问题。
一般在应用动协议设计上,数据建议都用字符型。也就是说用字符串进行传送,虽然会有效率问题,但是可用性大大提高。
否则的话,程序员要对不同平台的数字表示方式很清楚才行。
一般在应用动协议设计上,数据建议都用字符型。也就是说用字符串进行传送,虽然会有效率问题,但是可用性大大提高。
否则的话,程序员要对不同平台的数字表示方式很清楚才行。
#10
BufferedReader
#11
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.net;
import java.io.*;
import java.net.*;
/**
* This class implements a simple single-threaded proxy server.
**/
public class SimpleProxyServer {
/** The main method parses arguments and passes them to runServer */
public static void main(String[] args) throws IOException {
try {
// Check the number of arguments
if (args.length != 3)
throw new IllegalArgumentException("Wrong number of args.");
// Get the command-line arguments: the host and port we are proxy
// for and the local port that we listen for connections on.
String host = args[0];
int remoteport = Integer.parseInt(args[1]);
int localport = Integer.parseInt(args[2]);
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" +
remoteport + " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java SimpleProxyServer " +
"<host> <remoteport> <localport>");
}
}
/**
* This method runs a single-threaded proxy server for
* host:remoteport on the specified local port. It never returns.
**/
public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
// Create buffers for client-to-server and server-to-client transfer.
// We make one final so it can be used in an anonymous class below.
// Note the assumptions about the volume of traffic in each direction.
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
// This is a server that never returns, so enter an infinite loop.
while(true) {
// Variables to hold the sockets to the client and to the server.
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
// Get client streams. Make them final so they can
// be used in the anonymous thread below.
final InputStream from_client = client.getInputStream();
final OutputStream to_client = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try { server = new Socket(host, remoteport); }
catch (IOException e) {
PrintWriter out = new PrintWriter(to_client);
out.print("Proxy server cannot connect to " + host + ":"+
remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}
// Get server streams.
final InputStream from_server = server.getInputStream();
final OutputStream to_server = server.getOutputStream();
// Make a thread to read the client's requests and pass them
// to the server. We have to use a separate thread because
// requests and responses may be asynchronous.
Thread t = new Thread() {
public void run() {
int bytes_read;
try {
while((bytes_read=from_client.read(request))!=-1) {
to_server.write(request, 0, bytes_read);
to_server.flush();
}
}
catch (IOException e) {}
// the client closed the connection to us, so close our
// connection to the server. This will also cause the
// server-to-client loop in the main thread exit.
try {to_server.close();} catch (IOException e) {}
}
};
// Start the client-to-server request thread running
t.start();
// Meanwhile, in the main thread, read the server's responses
// and pass them back to the client. This will be done in
// parallel with the client-to-server request thread above.
int bytes_read;
try {
while((bytes_read = from_server.read(reply)) != -1) {
to_client.write(reply, 0, bytes_read);
to_client.flush();
}
}
catch(IOException e) {}
// The server closed its connection to us, so we close our
// connection to our client.
// This will make the other thread exit.
to_client.close();
}
catch (IOException e) { System.err.println(e); }
finally { // Close the sockets no matter what happens.
try {
if (server != null) server.close();
if (client != null) client.close();
}
catch(IOException e) {}
}
}
}
}
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.net;
import java.io.*;
import java.net.*;
/**
* This class implements a simple single-threaded proxy server.
**/
public class SimpleProxyServer {
/** The main method parses arguments and passes them to runServer */
public static void main(String[] args) throws IOException {
try {
// Check the number of arguments
if (args.length != 3)
throw new IllegalArgumentException("Wrong number of args.");
// Get the command-line arguments: the host and port we are proxy
// for and the local port that we listen for connections on.
String host = args[0];
int remoteport = Integer.parseInt(args[1]);
int localport = Integer.parseInt(args[2]);
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" +
remoteport + " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java SimpleProxyServer " +
"<host> <remoteport> <localport>");
}
}
/**
* This method runs a single-threaded proxy server for
* host:remoteport on the specified local port. It never returns.
**/
public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
// Create buffers for client-to-server and server-to-client transfer.
// We make one final so it can be used in an anonymous class below.
// Note the assumptions about the volume of traffic in each direction.
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
// This is a server that never returns, so enter an infinite loop.
while(true) {
// Variables to hold the sockets to the client and to the server.
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
// Get client streams. Make them final so they can
// be used in the anonymous thread below.
final InputStream from_client = client.getInputStream();
final OutputStream to_client = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try { server = new Socket(host, remoteport); }
catch (IOException e) {
PrintWriter out = new PrintWriter(to_client);
out.print("Proxy server cannot connect to " + host + ":"+
remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}
// Get server streams.
final InputStream from_server = server.getInputStream();
final OutputStream to_server = server.getOutputStream();
// Make a thread to read the client's requests and pass them
// to the server. We have to use a separate thread because
// requests and responses may be asynchronous.
Thread t = new Thread() {
public void run() {
int bytes_read;
try {
while((bytes_read=from_client.read(request))!=-1) {
to_server.write(request, 0, bytes_read);
to_server.flush();
}
}
catch (IOException e) {}
// the client closed the connection to us, so close our
// connection to the server. This will also cause the
// server-to-client loop in the main thread exit.
try {to_server.close();} catch (IOException e) {}
}
};
// Start the client-to-server request thread running
t.start();
// Meanwhile, in the main thread, read the server's responses
// and pass them back to the client. This will be done in
// parallel with the client-to-server request thread above.
int bytes_read;
try {
while((bytes_read = from_server.read(reply)) != -1) {
to_client.write(reply, 0, bytes_read);
to_client.flush();
}
}
catch(IOException e) {}
// The server closed its connection to us, so we close our
// connection to our client.
// This will make the other thread exit.
to_client.close();
}
catch (IOException e) { System.err.println(e); }
finally { // Close the sockets no matter what happens.
try {
if (server != null) server.close();
if (client != null) client.close();
}
catch(IOException e) {}
}
}
}
}
#12
socket通信设计:
可以自己定义一个适合工程需要的传输协议,所有的信息按照协议去作。
相当于自己去作一个Buffer。
如果与c通信最好是作个字节Buffer了。
可以自己定义一个适合工程需要的传输协议,所有的信息按照协议去作。
相当于自己去作一个Buffer。
如果与c通信最好是作个字节Buffer了。
#13
怎么跟我遇到的问题差不多,转换成字节串,想是应该可以的,不过我现在还没做测试用例。因为我的基础也不幸,还得看看字节串的用法,和一些I/O操作。你有什么好消息,告诉我!当然我如果有办法了,第一时间会告诉你的。
#14
看来只能一次次的写。
因为目前还没有什么类似于结构的类型可以放许多不同的数据类型,然后在一次性写过去。
目前只能用这种笨办法。
我现在用的是windows to aix, aix to aix 通信,不用高低转换。
因为java中就是big_endian,和aix中的统一,也就免了高低转换。
即使要高低转换,我在网上看到过别人写的一个过滤器类,也不复杂。
因为目前还没有什么类似于结构的类型可以放许多不同的数据类型,然后在一次性写过去。
目前只能用这种笨办法。
我现在用的是windows to aix, aix to aix 通信,不用高低转换。
因为java中就是big_endian,和aix中的统一,也就免了高低转换。
即使要高低转换,我在网上看到过别人写的一个过滤器类,也不复杂。
#1
实例没有,但是思路有一个:你为什么不把它们打入一个class中,一次性的传过去呢?
writeObject/readObject即可。
读出来时再造型一下,如果你是用java与java通讯,这种方法没问题,如果是java与c通讯,就不行了。
writeObject/readObject即可。
读出来时再造型一下,如果你是用java与java通讯,这种方法没问题,如果是java与c通讯,就不行了。
#2
严重同意楼上的说法!
实际应用常常把预先要传的数据封装在一个class中,你可以自定义,也可以用cllection,ArrayList之类的,方法很多,然后用writeObject/readObject来处理。一个数据一个数据的write,不是很好。
实际应用常常把预先要传的数据封装在一个class中,你可以自定义,也可以用cllection,ArrayList之类的,方法很多,然后用writeObject/readObject来处理。一个数据一个数据的write,不是很好。
#3
偶就是与c通信呀,还的高低转换,ft,继续等待大虾回复了
#4
晕倒
外面套一个BufferedInputStream就可以提高效率了
外面套一个BufferedInputStream就可以提高效率了
#5
呵呵,套个BufferedOutputStream,不过与c通讯很麻烦的
#6
有什么好晕的,套bufferInputStream我也知道,可我就要“打到一个BUFFER里再一次性的write出去”,怎么做嘛?!
#7
比如我举个土方法,不过肯定有问题,只是我所表达希望达到的就是这个大概意思:
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
真正具体实现方法如何?应该是基础问题了吧,可惜我基础不好
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
真正具体实现方法如何?应该是基础问题了吧,可惜我基础不好
#8
比如我举个土方法,不过肯定有问题,只是我所表达希望达到的就是这个大概意思:
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
void Req(byte MSG_HEAD,short command, byte param1, byte param2, byte param3, byte param4){
String temp = new String();
temp += String.valueOf((char)MSG_HEAD);
temp += String.valueOf(command);
temp += String.valueOf((char)param1);
temp += String.valueOf((char)param2);
temp += String.valueOf((char)param3);
temp += String.valueOf((char)param4);
temp += String.valueOf((char)(0-MSG_HEAD-command-param1-param2-param3-param4));
byte[] t = temp.getBytes();
index.output.write(t);
}
#9
你的应用层协议设计有问题,必然导致这个问题。
一般在应用动协议设计上,数据建议都用字符型。也就是说用字符串进行传送,虽然会有效率问题,但是可用性大大提高。
否则的话,程序员要对不同平台的数字表示方式很清楚才行。
一般在应用动协议设计上,数据建议都用字符型。也就是说用字符串进行传送,虽然会有效率问题,但是可用性大大提高。
否则的话,程序员要对不同平台的数字表示方式很清楚才行。
#10
BufferedReader
#11
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.net;
import java.io.*;
import java.net.*;
/**
* This class implements a simple single-threaded proxy server.
**/
public class SimpleProxyServer {
/** The main method parses arguments and passes them to runServer */
public static void main(String[] args) throws IOException {
try {
// Check the number of arguments
if (args.length != 3)
throw new IllegalArgumentException("Wrong number of args.");
// Get the command-line arguments: the host and port we are proxy
// for and the local port that we listen for connections on.
String host = args[0];
int remoteport = Integer.parseInt(args[1]);
int localport = Integer.parseInt(args[2]);
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" +
remoteport + " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java SimpleProxyServer " +
"<host> <remoteport> <localport>");
}
}
/**
* This method runs a single-threaded proxy server for
* host:remoteport on the specified local port. It never returns.
**/
public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
// Create buffers for client-to-server and server-to-client transfer.
// We make one final so it can be used in an anonymous class below.
// Note the assumptions about the volume of traffic in each direction.
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
// This is a server that never returns, so enter an infinite loop.
while(true) {
// Variables to hold the sockets to the client and to the server.
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
// Get client streams. Make them final so they can
// be used in the anonymous thread below.
final InputStream from_client = client.getInputStream();
final OutputStream to_client = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try { server = new Socket(host, remoteport); }
catch (IOException e) {
PrintWriter out = new PrintWriter(to_client);
out.print("Proxy server cannot connect to " + host + ":"+
remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}
// Get server streams.
final InputStream from_server = server.getInputStream();
final OutputStream to_server = server.getOutputStream();
// Make a thread to read the client's requests and pass them
// to the server. We have to use a separate thread because
// requests and responses may be asynchronous.
Thread t = new Thread() {
public void run() {
int bytes_read;
try {
while((bytes_read=from_client.read(request))!=-1) {
to_server.write(request, 0, bytes_read);
to_server.flush();
}
}
catch (IOException e) {}
// the client closed the connection to us, so close our
// connection to the server. This will also cause the
// server-to-client loop in the main thread exit.
try {to_server.close();} catch (IOException e) {}
}
};
// Start the client-to-server request thread running
t.start();
// Meanwhile, in the main thread, read the server's responses
// and pass them back to the client. This will be done in
// parallel with the client-to-server request thread above.
int bytes_read;
try {
while((bytes_read = from_server.read(reply)) != -1) {
to_client.write(reply, 0, bytes_read);
to_client.flush();
}
}
catch(IOException e) {}
// The server closed its connection to us, so we close our
// connection to our client.
// This will make the other thread exit.
to_client.close();
}
catch (IOException e) { System.err.println(e); }
finally { // Close the sockets no matter what happens.
try {
if (server != null) server.close();
if (client != null) client.close();
}
catch(IOException e) {}
}
}
}
}
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.net;
import java.io.*;
import java.net.*;
/**
* This class implements a simple single-threaded proxy server.
**/
public class SimpleProxyServer {
/** The main method parses arguments and passes them to runServer */
public static void main(String[] args) throws IOException {
try {
// Check the number of arguments
if (args.length != 3)
throw new IllegalArgumentException("Wrong number of args.");
// Get the command-line arguments: the host and port we are proxy
// for and the local port that we listen for connections on.
String host = args[0];
int remoteport = Integer.parseInt(args[1]);
int localport = Integer.parseInt(args[2]);
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" +
remoteport + " on port " + localport);
// And start running the server
runServer(host, remoteport, localport); // never returns
}
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java SimpleProxyServer " +
"<host> <remoteport> <localport>");
}
}
/**
* This method runs a single-threaded proxy server for
* host:remoteport on the specified local port. It never returns.
**/
public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
// Create buffers for client-to-server and server-to-client transfer.
// We make one final so it can be used in an anonymous class below.
// Note the assumptions about the volume of traffic in each direction.
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
// This is a server that never returns, so enter an infinite loop.
while(true) {
// Variables to hold the sockets to the client and to the server.
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
// Get client streams. Make them final so they can
// be used in the anonymous thread below.
final InputStream from_client = client.getInputStream();
final OutputStream to_client = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try { server = new Socket(host, remoteport); }
catch (IOException e) {
PrintWriter out = new PrintWriter(to_client);
out.print("Proxy server cannot connect to " + host + ":"+
remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}
// Get server streams.
final InputStream from_server = server.getInputStream();
final OutputStream to_server = server.getOutputStream();
// Make a thread to read the client's requests and pass them
// to the server. We have to use a separate thread because
// requests and responses may be asynchronous.
Thread t = new Thread() {
public void run() {
int bytes_read;
try {
while((bytes_read=from_client.read(request))!=-1) {
to_server.write(request, 0, bytes_read);
to_server.flush();
}
}
catch (IOException e) {}
// the client closed the connection to us, so close our
// connection to the server. This will also cause the
// server-to-client loop in the main thread exit.
try {to_server.close();} catch (IOException e) {}
}
};
// Start the client-to-server request thread running
t.start();
// Meanwhile, in the main thread, read the server's responses
// and pass them back to the client. This will be done in
// parallel with the client-to-server request thread above.
int bytes_read;
try {
while((bytes_read = from_server.read(reply)) != -1) {
to_client.write(reply, 0, bytes_read);
to_client.flush();
}
}
catch(IOException e) {}
// The server closed its connection to us, so we close our
// connection to our client.
// This will make the other thread exit.
to_client.close();
}
catch (IOException e) { System.err.println(e); }
finally { // Close the sockets no matter what happens.
try {
if (server != null) server.close();
if (client != null) client.close();
}
catch(IOException e) {}
}
}
}
}
#12
socket通信设计:
可以自己定义一个适合工程需要的传输协议,所有的信息按照协议去作。
相当于自己去作一个Buffer。
如果与c通信最好是作个字节Buffer了。
可以自己定义一个适合工程需要的传输协议,所有的信息按照协议去作。
相当于自己去作一个Buffer。
如果与c通信最好是作个字节Buffer了。
#13
怎么跟我遇到的问题差不多,转换成字节串,想是应该可以的,不过我现在还没做测试用例。因为我的基础也不幸,还得看看字节串的用法,和一些I/O操作。你有什么好消息,告诉我!当然我如果有办法了,第一时间会告诉你的。
#14
看来只能一次次的写。
因为目前还没有什么类似于结构的类型可以放许多不同的数据类型,然后在一次性写过去。
目前只能用这种笨办法。
我现在用的是windows to aix, aix to aix 通信,不用高低转换。
因为java中就是big_endian,和aix中的统一,也就免了高低转换。
即使要高低转换,我在网上看到过别人写的一个过滤器类,也不复杂。
因为目前还没有什么类似于结构的类型可以放许多不同的数据类型,然后在一次性写过去。
目前只能用这种笨办法。
我现在用的是windows to aix, aix to aix 通信,不用高低转换。
因为java中就是big_endian,和aix中的统一,也就免了高低转换。
即使要高低转换,我在网上看到过别人写的一个过滤器类,也不复杂。