[英]not able to send byte array from server to client,able to send from client to server
我有一个任务要做。 创建一个客户端和服务器套接字交互,该交互接受字节数据并转换字符串中服务器接收的字节数据,并通过成功/不成功的数据对话确认发送回响应,因为传递的数据将具有固定数据长度格式因此,验证应在服务器端进行。
至于例如
您向服务器发送的字段有一些,
字段1-数字字段2-字符串字段3为浮点数,即108.50
从字节转换为字符串后:152 |任意msg | 108.50
在字节中将是这样,
10101 | 1001010010000000011000000000 | 1110111011
我已经尝试了以下程序来做到这一点
服务器.java
public class Server extends Thread
{
private ServerSocket serverSocket;
public Server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(100000);
}
public void run()
{
while(true)
{
try
{
Socket server = serverSocket.accept();
byte Message[]=null;
DataInputStream in =
new DataInputStream(server.getInputStream());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
System.out.println("On this line"); //This doesnt get printed
buffer.flush();
data= buffer.toByteArray();
System.out.println(data);
String convertmsg = new String(data);
System.out.println("Msg converted "+convertmsg);
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
System.out.println("below dataoutputstream");
out.write("Success".getBytes());
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = 4003;
try
{
Thread t = new Server(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
客户
public class Client {
public static void main(String args[]) throws IOException
{
int userinput =1;
while(userinput==1)
{
String serverName = "192.168.0.8";
int port = 4003;
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out =
new DataOutputStream(outToServer);
System.out.println("above out.wirte()");
out.write("any msg".getBytes());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
System.out.println("converting array "+in);
byte[] data = IOUtils.toByteArray(in);
System.out.println(data);//This line doesnt get printed
//System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("Enter userinput ");
DataInputStream dis = new DataInputStream(System.in);
String s = dis.readLine();
userinput = Integer.parseInt(s);
}
}
}
如果我以字节为单位将数据从客户端发送到服务器,它将读取并打印它。然后将打印“ Enter userinput”行,如果用户输入“ 1”,程序将继续。 但是问题是上面给出的这个程序。 如果我尝试从服务器发送数据,说明“成功”(表示数据已成功从字节转换为字符串),则程序卡住,光标不会移至注释“此行未打印”行下方。没有错误打印并且程序都没有终止。我是套接字编程的新手,对网络了解不多。 任何帮助将不胜感激。
您正在读取输入,直到流结束,但是对等方并没有关闭连接,因此流末永远不会到达。
我建议您读写行,或使用writeUTF()和readUTF()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.