Socket问题,网络可以连接上,但是返回值接收不到,求助!

hrbfanxinghe 2014-03-13 10:12:09
Socket socket = new Socket(ip, port);
System.out.println("服务器连接成功");
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);
pw.println(msg+"\r\n\r\n");
pw.flush()

// 接收返回的结果
StringBuffer buffer = new StringBuffer();
InputStreamReader inputStreamReader=new InputStreamReader(socket.getInputStream());

BufferedReader rd = new BufferedReader(inputStreamReader);

String line;
while ((line = rd.readLine()) != null) {
String t = new String(line.getBytes(), "utf-8");
buffer.append(t);
}
strRst = buffer.toString();

pw.close();
rd.close();
osw.close();
os.close();
socket.close();


我与对方的网络是可以通的,但是接收返回值得时候rd.readLine()一直为null的,求助!!
...全文
560 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hrbfanxinghe 2014-03-17
  • 打赏
  • 举报
回复
String msg = "PHONE=15270018560&AGENT=&BEGD=20140301&ENDD=201340301;"; String[] str = {"FFFF","36","09","5A","00","00","31","30"}; char[] result = new char[8]; for (int i = 0; i < str.length; i++) { result[i] = (char)Integer.parseInt(str[i], 16); System.out.println(result[i]); } System.out.println(msg); PrintWriter out = new PrintWriter( new FileWriter( "D:\\1.txt" ) ); out.write(result[0]); out.write(result[1]); out.write(result[2]); out.write(result[3]); out.write(result[4]); out.write(result[5]); out.write("1000"); out.write("156 "); out.write(" "); out.write(" "); out.write(result[6]); out.write("; "); out.write("& "); out.write(" "); out.write(" "); out.write(msg); out.write(result[0]); out.close(); 各位大神帮看一下呗,我输入了几个十六进制字符串,然后转成ASCII码再插入到文本里,可以转的,但是我用JAVA不同的类,转完后FFFF这个竟然是不一样的,PrintWriter类和File类的文本输出就不一样的,我需要的是File这个类生成文件的内容,但是生成后每个字符前多了一个空格,求指教
wyx100 2014-03-13
  • 打赏
  • 举报
回复
服务端客户端对应,检查一下
tony4geek 2014-03-13
  • 打赏
  • 举报
回复
hrbfanxinghe 2014-03-13
  • 打赏
  • 举报
回复
你的意思是,我要跟对方的报文格式进行一致吗
Defonds 2014-03-13
  • 打赏
  • 举报
回复
发送端怎么发送的,最好要一致
Defonds 2014-03-13
  • 打赏
  • 举报
回复
给你个例子看看。 发送端代码:
import java.net.*;
import java.io.*;

/**
 * UDPSender is an implementation of the Sender interface, using UDP as the transport protocol.
 * The object is bound to a specified receiver host and port when created, and is able to 
 * send the contents of a file to this receiver.
 *
 * @author Alex Andersen (alex@daimi.au.dk)
 */
public class TCPSender implements Sender{
    private File theFile;
    private FileInputStream fileReader;
    private Socket s;
    private int fileLength, currentPos, bytesRead, toPort, length;
    private byte[]  msg, buffer;
    private String toHost,initReply;
    private InetAddress toAddress;
    private OutputStream theOutstream; 
    private InputStream theInstream;

    /**
     * Class constructor.
     * Creates a new UDPSender object capable of sending a file to the specified address and port.
     *
     * @param address  the address of the receiving host
     * @param port    the listening port on the receiving host
     */
    public TCPSender(InetAddress address, int port) throws IOException{
	toPort = port;
	toAddress = address;
	msg = new byte[8192];
	buffer = new byte[8192];
	s = new Socket(toAddress, toPort);
	theOutstream = s.getOutputStream();
	theInstream = s.getInputStream();
    }
    

    /**
     * Sends a file to the bound host.
     * Reads the contents of the specified file, and sends it via UDP to the host 
     * and port specified at when the object was created.
     *
     * @param theFile  the file to send
     */
    public void sendFile(File theFile) throws IOException{
	// Init stuff
	fileReader = new FileInputStream(theFile);
	fileLength = fileReader.available();
	
	System.out.println(" -- Filename: "+theFile.getName());
	System.out.println(" -- Bytes to send: "+fileLength);

	// 1. Send the filename and length to the receiver
	theOutstream.write((theFile.getName()+"::"+fileLength).getBytes());
	theOutstream.flush();


	// 2. Wait for a reply from the receiver	
	System.out.println(" -- Waiting for OK from the receiver");
	length = 0;
	while (length <= 0){
	    length = theInstream.read(buffer);
	    if (length>0) initReply = (new String(buffer, 0, length));
	}
	
	
	// 3. Send the content of the file
	if (initReply.equals("OK"))
	    {
		System.out.println("  -- Got OK from receiver - sending the file ");

		
		while (currentPos<fileLength){
		    //System.out.println("Will read at pos: "+currentPos);
		    bytesRead = fileReader.read(msg);
		    theOutstream.write(msg);
		    //System.out.println("Bytes read: "+bytesRead);
		    currentPos = currentPos + bytesRead;
		}
		
		
		System.out.println("  -- File transfer complete...");
	    }
	else{System.out.println("  -- Recieved something other than OK... exiting");}
    }
}
接收端代码:
import java.net.*;
import java.io.*;
import java.util.*;

public class TCPReceiver{
    int length;
    ServerSocket listener;
    Socket s;
    String filename, initString;
    byte[] buffer;
    FileOutputStream fileWriter;
    int bytesReceived, bytesToReceive;
    InputStream theInstream;
    OutputStream theOutstream;

    public TCPReceiver(int port) throws IOException
    {
	// Init stuff
	listener = new ServerSocket(port);
	buffer = new byte[8192];	
	
	System.out.println(" -- Ready to receive file on port: "+port);
	
	s = listener.accept();	
	theInstream = s.getInputStream();
	theOutstream = s.getOutputStream();

	// 1. Wait for a sender to transmit the filename

	length = theInstream.read(buffer);
	initString = "Recieved-"+new String(buffer, 0, length);
	StringTokenizer t = new StringTokenizer(initString, "::");
	filename = t.nextToken();
	bytesToReceive = new Integer(t.nextToken()).intValue();
	
	System.out.println("  -- The file will be saved as: "+filename);
	System.out.println("  -- Expecting to receive: "+bytesToReceive+" bytes");
	
	
	// 2. Send an reply containing OK to the sender
	theOutstream.write((new String("OK")).getBytes());
	System.out.println("send something");
	
	
	// 3. Receive the contents of the file
	 fileWriter = new FileOutputStream(filename);
		
	while(bytesReceived < bytesToReceive)
	    {
		length = theInstream.read(buffer);
		fileWriter.write(buffer, 0,  length);
		bytesReceived = bytesReceived + length;
	    }
	System.out.println("  -- File transfer complete.");
    }   
}

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧