上位機之網絡編程Socket調試(一)


調試網絡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("\u7F51\u7EDC\u8FDE\u63A5");
        
        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("\u4E0B\u4F4D\u673AIP\u8BBE\u7F6E");
        
        Label lblip = new Label(grpip, SWT.NONE);
        lblip.setFont(SWTResourceManager.getFont("宋體", 12, SWT.NORMAL));
        lblip.setBounds(52, 75, 71, 31);
        lblip.setText("IP\u5730\u5740\uFF1A");
        
        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("\u8FDE\u63A5");
        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,"連接狀態",'\n'+"已成功連接!");
                //    System.out.println(ipAddress);
                    connectButton.setEnabled(false);
                }
                else 
                    MessageDialog.openInformation(shell,"連接狀態",'\n'+"連接失敗!");
            }

            @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("\u65AD\u5F00");
        unconnectButton.setBounds(254, 150, 92, 31);
    }
    
    public static String getIpAddress() {
        ipAddress = ipInput.getText();
        return ipAddress;
    }
}

界面效果圖為下圖所示。

 

目前已能夠根據ip地址和端口號給下位機(ARM)發送數據,數據反饋部分未做。另外,數據發送的具體形式未做。

現在出現的問題:Socket程序中調用輸入數據的等相關函數時,程序會卡死,原因未知,或許涉及到一些線程的問題。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM