java中的双向客户端服务器通信 [英] Bidirectional Client Server Communication in java

查看:97
本文介绍了java中的双向客户端服务器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai ...我尝试了简单的双向客户端服务器通信。但我无法执行双向操作。只有我能够从客户端接收文本文件到服务器。在双向通信的情况下,我需要发送回相同的文件响应客户。任何人都可以请帮助我

Hai...I tried simple bidirectional client server communication.But i cant able to perform bidirectional.Only i have able to receive text file from client to server.In case of bidirectional communication i need to send back the same file as response to the client.Anyone can please help me

推荐答案

Hai



请检查以下代码和让我知道你的建议。这里我只能进行单向通信..不能获得与服务器响应相同的文件..



提前谢谢



服务器代码:



Hai

Please check the below code and let me know your suggestions.Here i am only able to perform one way communication..not get the same file as response from server..

Thanks in advance

Server code:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server
{
 
 private static Socket socket;
 static int current=0;
 public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text1.txt";
 public final static int FILE_SIZE = 4939993 ;
    public static void main(String[] args)
    {
        try
        {
 
            int port = 6777;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port 6777");
 
            //Server is running always. This is done using this while(true) loop
            
             while(true)
            {
                //Reading the message from the client
                
                 socket = serverSocket.accept();
                 InputStream is = socket.getInputStream();
                 FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
                 BufferedOutputStream bos=new BufferedOutputStream(fos);
                 byte [] mybytearray  = new byte [FILE_SIZE];
                 int bytesRead = is.read(mybytearray,0,mybytearray.length);
       	         current = bytesRead;
                 do {
        	         bytesRead =
        	            is.read(mybytearray, current, (mybytearray.length-current));
        	         if(bytesRead >= 0) current += bytesRead;
        	      } while(bytesRead > -1);

        	      bos.write(mybytearray, 0 , current);
        	      //bos.close();
        	     
               System.out.println("Message received from client is "+current);
             
 
                //Sending the response back to the client.
               OutputStream  out = socket.getOutputStream();
               File myFile=new File( FILE_TO_RECEIVED);
               FileInputStream fis=new FileInputStream(myFile);
               byte [] mybytearray1  = new byte [(int)myFile.length()];
              
             
               int len ;
	            while ((len = fis.read(mybytearray1)) >= 0) {
	            	out.write(len);
	            	System.out.println("Message sent to the client is "+ current);
	            	out.flush();
	          	    //fis.close();
	          	   
         	    
         }
            
        }}
        catch (Exception e)
        {
            e.printStackTrace();
            
        }
        finally
        {
            try
            { 
            	socket.close();
                
               }
            catch(Exception e){
            	e.printStackTrace();
            }
        }
    }
	
}





客户代码:





Client Code:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
 
public class Client
{
    public final static String FILE_TO_SEND = "/home/sosdt010/Desktop/doc/character.txt";
    private static Socket socket;
    public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text2.txt";
    public final static int FILE_SIZE = 4939993 ;
    static int current=0;
    public static void main(String args[])
    {
        try
        {
            String host = "10.170.0.38";
            int port = 6777;
            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(address, port);
 
            //Send the message to the server
            OutputStream  out = socket.getOutputStream();
            File myFile=new File( FILE_TO_SEND);
            FileInputStream fis=new FileInputStream(myFile);
            BufferedInputStream bis=new BufferedInputStream(fis);
            byte [] mybytearray1  = new byte [(int)myFile.length()];
          
            int len ;
	            while ((len = fis.read(mybytearray1)) >= 0) {
      	  	 out.write(mybytearray1,0,len);
      	  	//out.close();
      	    out.flush();
      	    //fis.close();
      	    //bis.close();
      	   
      	    System.out.println("Message sent to the server is "+ len);
      
      	 
        }
            
  
            //Get the return message from the server
	            InputStream is = socket.getInputStream();
                FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
                BufferedOutputStream bos=new BufferedOutputStream(fos);
                byte [] mybytearray  = new byte [FILE_SIZE];
                int bytesRead = is.read(mybytearray,0,mybytearray.length);
      	      current = bytesRead;
                do {
       	         bytesRead =
       	            is.read(mybytearray, current, (mybytearray.length-current));
       	         if(bytesRead >= 0) current += bytesRead;
       	      } while(bytesRead > -1);

       	      bos.write(mybytearray, 0 , current);
       	      //bos.close();
              System.out.println("Message received from server is "+current);
            
          
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        finally
        {
            //Closing the socket
            try
            {
                socket.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
	
}


这篇关于java中的双向客户端服务器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆