getOutputStream() in java.net.Socket cannot be applied to (byte[])小弟很菜,请继续指教,
OutputStream os = socket.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); byte[] bytesToSend = ...; bos.write(bytesToSend,0,bytesToSend.length);
public void write(byte[] b)throws IOException API里写的这么清楚,怎么叫:“getOutputStream() in java.net.Socket cannot be applied to (byte[])”应该是: OutputStream out = socket.getOutputStream(); out.write(你的byte数组!); out.close;
outFile=new PrintWriter(ds.getOutputStream(),true);
out.println("150 Ready.") ;
for(int i=0;i<yourStrings.length ;i++){
outFile.println(yourStrings[i]);}
outFile.flush() ;
outFile.close() ;
这样就可以了
import java.io.*;
import java.net.*;public class SocketServer {
ServerSocket ss=null;
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;public SocketServer() {
try{
init();
}
catch(Exception e){
System.out.println(e.toString());
}
}void init() throws Exception{
ss=new ServerSocket(765);
s.setSoTimeout(3000);
}void waitForClient(){
try{
s=ss.accept();
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
outStream.writeUTF("1");
s.setSoTimeout(3000);
waitData();
}
catch(Exception e){
System.out.println(e.toString());
}
}void waitData(){
while(true){
try{
String str=inStream.readUTF();
System.out.println("Server accept: "+str);
int nu=Integer.parseInt(str)+1;
if(nu>20){
System.out.println("Send end!");
break;
}
else{
str=Integer.toString(nu);
outStream.writeUTF(str);
}
}
catch(Exception e){
System.out.println(e.toString());
break;
}
}
}public static void main(String[] args) {
SocketServer socketServer1 = new SocketServer();
socketServer1.waitForClient();
}
}客户端的程序:
import java.net.*;
import java.io.*;public class SocketClient{
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;public SocketClient() {
try{
init();
waitData();
}
catch(Exception e){
System.out.println(e.toString());
}
}void init() throws Exception{
s=new Socket("192.168.0.32",765); //把这里的IP改成你运行SocketServer.class的IP
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
s.setSoTimeout(3000);
}void waitData(){
while(true){
try{
String str=inStream.readUTF();
System.out.println("Client accept: "+str);
str=Integer.toString(Integer.parseInt(str)+1);
outStream.writeUTF(str);
}
catch(Exception e){
System.out.println(e.toString());
break;
}
}
}public static void main(String[] args) {
SocketClient socketClient1 = new SocketClient();
}
}
我要发的是Byte数组,不是string数组,用
out=new BufferedWriter(new OutputStreamWriter (s.getOutputStream()));
out.write()方法不能传Byte数组,
有什么办法吗
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] bytesToSend = ...;
bos.write(bytesToSend,0,bytesToSend.length);
API里写的这么清楚,怎么叫:“getOutputStream() in java.net.Socket cannot be applied to (byte[])”应该是:
OutputStream out = socket.getOutputStream();
out.write(你的byte数组!);
out.close;