问题描述
- JAVA客户端与服务端通信问题 25C
-
public void sendCMD(final byte[] bufferfinal String ip){ new Thread(new Runnable() { @Override public void run() { Socket socket=null; try{ socket = new Socket(InetAddress.getByName(ip) Proxys.PORT_RECEIVE + Max); InputStream inputStream = new ByteArrayInputStream(buffer); OutputStream outputStream = socket.getOutputStream(); byte buffer [] = new byte[1024]; int temp=0; while((temp = inputStream.read(buffer)) != -1){ outputStream.write(buffer0temp);} outputStream.flush(); } 客户端发送指令给服务端 public void run() { // 消息循环 ServerSocket serverSocket = null; Socket socket = null; try { //定义TCP监听 serverSocket = new ServerSocket(Proxys.PORT_RECEIVE + con.Max); socket = serverSocket.accept(); int temp=0; //定义缓冲区 byte[] buffer=new byte[1024]; String str=""""; //定义接收数据包 InputStream inputStream = socket.getInputStream(); while ((temp = inputStream.read(buffer)) != -1) { str=new String(buffer 0 temp);} 服务端接收客户端发送的流 不知道哪个地方有问题,请大神看一下上面贴上的代码是否有问题 谢谢
解决方案
代码太乱。能否贴上完整的代码?
客户端
public class DayTimeClient { //请求服务器,获得信息并输出 public static void main(String[] args) { try (Socket socket = new Socket(""localhost""13)) { socket.setSoTimeout(15000); InputStream in = socket.getInputStream(); StringBuilder sb = new StringBuilder(); InputStreamReader reader = new InputStreamReader(inASCII""); for (int c = reader.read(); c != -1 ; c = reader.read()) { sb.append((char) c); } System.out.println(sb); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }//end main()}
服务器
public class DayTimeServer { public static void main(String[] args) { DayTimeServer dayTimeServer = new DayTimeServer(); dayTimeServer.start(); }//end main() public void start(){ //线程池 ExecutorService pool = Executors.newFixedThreadPool(50); //服务器Socket ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(13); while(true){ //获取请求信息Socket Socket connection = serverSocket.accept(); Callable<Void> task = new DayTimeTask(connection); pool.submit(task); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(serverSocket != null) {serverSocket.close();} } catch (IOException e) {} } }//end test()}
服务器用到的一个线程类
public class DayTimeTask implements Callable<Void> { private Socket socket; public DayTimeTask (Socket socket) { this.socket = socket; }//end DayTimeTask() @Override public Void call() { try { Writer writer = new OutputStreamWriter(socket.getOutputStream()ASCII""); Date now = new Date(); writer.write(now.toString()+""rn""); writer.flush(); } catch(IOException e) { System.err.println(e); } finally { try { socket.close(); } catch (IOException e) { } } return null; }//end call()}
解决方案二:
给你个例子你参考一下吧
Server.java: import java.io.*;import java.net.*; public class Server{ publicstatic void main(String[] args){ try{ ServerSocket s = newServerSocket(9000); while (true) { Socket incoming =s.accept(); try{ FileInputStreamfstream = new FileInputStream(""Server.java""); //假设输入文件为Server.java DataInputStream in =new DataInputStream(fstream); BufferedReaderbr = new BufferedReader(new InputStreamReader(in)); OutputStreamostream = incoming.getOutputStream(); PrintWriterout = new PrintWriter(ostream true); String strLine; while ((strLine =br.readLine()) != null) { out.println(strLine); } in.close(); } finally { incoming.close(); } } }catch (Exception e){ e.printStackTrace(); } }}客户端Client.java: import java.io.*;import java.net.*;import java.util.*; public class Client{ publicstatic void main(String[] args){ try { Sockets = new Socket(""localhost"" 9000); try { InputStreaminStream = s.getInputStream(); Scannerin = new Scanner(inStream); PrintWriter out = newPrintWriter(""test.txt"");//假设输出文件为test.txt while(in.hasNextLine()){ Stringline = in.nextLine(); System.out.println(line); out.println(line); } out.close(); } finally { s.close(); } } catch(IOException e) { e.printStackTrace(); } }}
解决方案三:
你代码贴不不全,不过JAVA的socket通信网上有很多例子的,耐心看懂就ok了 没什么难的
解决方案四:
你具体有什么问题呢?
解决方案五:
你代码不全,好像是定义的变量是局部变量。到其他函数,就没有用了。
参照“PB人生”
解决方案六:
去看下我的博客,有个nio实现通信
解决方案七:
问题好多。你这个还会粘包
解决方案八:
问题好多。你这个还会粘包
时间: 2024-09-10 14:10:59