java Network programming using socket(1)
来源:互联网 发布:c语言编程简易小游戏 编辑:程序博客网 时间:2024/05/18 19:42
Java supports stream-based communication and packet-based communication,and the first is universal.
Create ServerSocket
1.Create ServerSocket
ServerSocket serverSocket=new ServerSocket(port);
2.Listen to the connect
Socket socket=serverSocket.accept();
3.We need create InputStream and OutputStream to transmit data
InputStream inputFromClient=socket.getInputSream();
OutputStream outputToClient=socket.getOutputStream();
(we can also use DataInputStream and DataOutputStream ,BufferedReader and PrintWriter to get the data types are int,double and string.)
Create client Socket
1.Create client Socket
Socket socket=new Socket(serverName,port);
(the serverName can be host name or the host IP address.)
2.We need create InputStream and OutputStream to transmit data
InputSream inputFromServer=socket.getInputStream();
OutputStream outputToServer=socket.getOutputStream();
(we can also use DataInputStream and DataOutputStream ,BufferedReader and PrintWriter to get the data type are int,double or string.)
Now,there is an example that Client send radius to Server and Server compute area and return to Client.
Server.java
import java.awt.BorderLayout;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class Server extends JFrame { private JTextArea jta=new JTextArea(); public static void main(String[] args) { new Server(); } public Server() { setLayout(new BorderLayout()); add(new JScrollPane(jta),BorderLayout.CENTER); setTitle("Server"); setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); try{ //Create a server socket ServerSocket serverSocket=new ServerSocket(8000); jta.append("Server started at "+new Date() +"\n"); //Listen for a connection request Socket socket=serverSocket.accept(); //Create data input and output streams DataInputStream inputFromClient=new DataInputStream(socket.getInputStream()); DataOutputStream outputToClient=new DataOutputStream(socket.getOutputStream()); while (true) { //Receive radius from the client double radius=inputFromClient.readDouble(); //Compute area double area=radius*radius*Math.PI; //Send area back to the client outputToClient.writeDouble(area); jta.append("Radius received from client:"+radius +'\n'); jta.append("Area found:" +area+'\n'); } } catch(IOException ex){ System.err.println(ex); } }}
Client.java
import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.InetAddress;import java.net.Socket;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class Client extends JFrame { //Text filed for receiving radius private JTextField jtf=new JTextField(); //Text area to display contents private JTextArea jta=new JTextArea(); //IO streams private DataOutputStream toServer; private DataInputStream fromServer; public static void main(String[] args) { new Client(); } public Client(){ //Panel panel to hold the label and text field JPanel panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Enter radius"),BorderLayout.WEST); panel.add(jtf,BorderLayout.CENTER); jtf.setHorizontalAlignment(JTextField.RIGHT); setLayout(new BorderLayout()); add(panel,BorderLayout.NORTH); add(new JScrollPane(jta),BorderLayout.CENTER); jtf.addActionListener(new TextFieldListener()); setTitle("Client"); setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);//It is necessary to show the frame here try { //Create a socket to connect to the server Socket socket=new Socket("localhost",8000); //Socket socket =new Socket("130.254.204.36",8000); //Socket socket =new Socket("drake.Armstrong.edu",8000); System.out.println("local port: "+socket.getLocalPort()); //Test InetAddress InetAddress inetAddress=socket.getInetAddress(); System.out.println("Client's host name is "+inetAddress.getHostName()); System.out.println("Client's IP Address is "+inetAddress.getHostAddress()); // //Create an input stream to receive data from the server fromServer=new DataInputStream(socket.getInputStream()); //Create an output stream to send data from the server toServer=new DataOutputStream(socket.getOutputStream()); } catch (IOException ex) { jta.append(ex.toString()+'\n'); } } private class TextFieldListener implements ActionListener{ public void actionPerformed(ActionEvent e) { try { //get the radius from the text field double radius=Double.parseDouble(jtf.getText().trim()); //Send the radius to the server toServer.writeDouble(radius); toServer.flush(); //Get area from the server double area=fromServer.readDouble(); //display to the text server jta.append("Radius is "+radius+"\n"); jta.append("Area received from the server is "+area +'\n'); } catch (IOException ex) { System.err.println(ex); } } }}
- java Network programming using socket(1)
- java Network programming using socket(1)
- java Network programming using socket(2)
- java Network programming using socket(2)
- Network socket programming using the NetLib library
- Network Programming Using Libevent
- socket network programming
- Java Network Programming 笔记(1)
- Java Network Programming 笔记(1)
- [转]Network Programming Using Libevent
- [转]Network Programming Using Libevent
- [转]Network Programming Using Libevent
- Unix Network Programming {socket 选项}
- programming interfaces using raw socket
- Java Network Game Programming
- Java Network Programming笔记
- Using epoll() For Asynchronous Network Programming
- 尝试《Java Network Programming 4ed》Chapter 8 Using Sockets翻译
- building an array from several ranges
- 第12周 程序阅读-多重继承3
- VS2008引用.lib静态库文件
- iTween曲线动画(沿着轨迹运动)
- SVM, Perceptron, LDA, Logistic Regression etc.
- java Network programming using socket(1)
- 2015 百度之星(资格)1003
- 11.0是春哥啊
- Unity3D项目文件目录发布后对应的ios/android应用目录
- win api GetLastError()转换成系统信息代码
- 推拿培训师王春亮谈成功法则----死死盯住一个目标
- 第12周 程序阅读-多重继承4
- 11.1存储班长学生的信息类
- 提高第32课时,自测