【发布时间】:2014-06-26 10:08:32
【问题描述】:
我有一个任务来做这件事。 创建一个客户端和服务器套接字交互,它接受字节数据并将服务器接收到的字节数据数据转换为字符串,并通过成功/不成功的数据对话确认发送回响应,因为传递的数据将具有固定数据长度格式所以验证应该在服务器端完成。
例如
有些字段是您发送到服务器的,例如,
字段 1 - 数字 字段 2 - 字符串 字段 3 为浮点数,即 108.50
从字节转换为字符串后: 152|任何消息|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);
}
}
}
如果我以字节为单位将数据从客户端发送到服务器,它会读取并打印它。此外,“输入用户输入”行也会被打印,如果用户输入“1”,程序将继续。 但问题是上面给出的这个程序。如果我尝试从服务器发送数据并声明“成功”(意味着数据已成功从字节转换为字符串),则程序卡住并且光标不会低于 cmets 中的行“此行未打印”。那里没有打印错误,也没有程序终止。我是套接字编程的新手,对网络不太了解。 任何帮助将不胜感激。
【问题讨论】:
-
此链接可能对您有所帮助:stackoverflow.com/questions/13596660/…
-
对不起,链接是关于一些图像传输的
-
堆栈溢出有什么问题。:(
标签: java sockets network-programming