调试网络socket的东西,下载一个调试助手相当有必要,可以起到事半工倍的效果。软件界面如下图所示。
在设计上位机时,需要利用java里的socket类中的相关函数编写一个客户端程序。经过调试,已可用,代码如下。
package person.hushunfeng;
/**
*
* @author hushunfeng
*
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
public class ClientSocket {
public static String clientName = "hushunfeng";
public static int portNO = 3333;
public static boolean connectStaus = false;
public ClientSocket() throws IOException { //这里设计的构造方法就是给这个类传ip地址。
// public static void main(String[] args) throws Exception {
// InetAddress ipAddress = InetAddress.getByName("localhost");
System.out.println(NetConnection.ipAddress);
Socket socket = new Socket(NetConnection.ipAddress,portNO);
try {
System.out.println("socketsever:"+socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
connectStaus = true;
System.out.println("已连接!!!");
out.println("ABCDE");
// out.println("byebye");
// String str = in.readLine();
// System.out.println(str);
} catch(Exception e) {
System.out.println("连接失败!!!");
System.out.println(e.getMessage());
}
finally {
System.out.println("close the Client socket and the io.");
socket.close();
}
}
}
设计的界面代码如下:
package person.hushunfeng; import java.io.IOException; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.SWT; import org.eclipse.wb.swt.SWTResourceManager; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Button; public class NetConnection { protected Shell shell; public static Text ipInput; /** * Launch the application. * @param args */ public static String ipAddress; public static void main(String[] args) { try { NetConnection window = new NetConnection(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ protected void createContents() { shell = new Shell(); shell.setSize(450, 262); shell.setText("u7F51u7EDCu8FDEu63A5"); Group grpip = new Group(shell, SWT.NONE); grpip.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL)); grpip.setBounds(10, 10, 422, 209); Label lblNewLabel = new Label(grpip, SWT.NONE); lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 14, SWT.NORMAL)); lblNewLabel.setBounds(148, 23, 185, 31); lblNewLabel.setText("u4E0Bu4F4Du673AIPu8BBEu7F6E"); Label lblip = new Label(grpip, SWT.NONE); lblip.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL)); lblip.setBounds(52, 75, 71, 31); lblip.setText("IPu5730u5740uFF1A"); ipInput = new Text(grpip, SWT.BORDER); ipInput.setBounds(129, 76, 191, 21); //"连接"按钮相关 final Button connectButton = new Button(grpip, SWT.NONE); connectButton.setTouchEnabled(true); connectButton.setBounds(62, 150, 92, 31); connectButton.setText("u8FDEu63A5"); connectButton.addMouseListener(new MouseListener(){ public void mouseDown(MouseEvent e) { getIpAddress(); try { new ClientSocket(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(ClientSocket.connectStaus==true) { MessageDialog.openInformation(shell,"连接状态",' '+"已成功连接!"); // System.out.println(ipAddress); connectButton.setEnabled(false); } else MessageDialog.openInformation(shell,"连接状态",' '+"连接失败!"); } @Override public void mouseDoubleClick(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseUp(MouseEvent e) { // TODO Auto-generated method stub } }); //"断开"按钮相关 final Button unconnectButton = new Button(grpip, SWT.NONE); unconnectButton.setEnabled(false); unconnectButton.setText("u65ADu5F00"); unconnectButton.setBounds(254, 150, 92, 31); } public static String getIpAddress() { ipAddress = ipInput.getText(); return ipAddress; } }
界面效果图为下图所示。
目前已能够根据ip地址和端口号给下位机(ARM)发送数据,数据反馈部分未做。另外,数据发送的具体形式未做。
现在出现的问题:Socket程序中调用输入数据的等相关函数时,程序会卡死,原因未知,或许涉及到一些线程的问题。