java基础--socket
来源:互联网 发布:windows查看cpu 编辑:程序博客网 时间:2024/06/03 18:35
java网络编程中一般用到的技术有:socket,IO,有时候也会用到图形界面。
URL
代表一个统一资源定位符,它是指向互联网“资源”的指针。一个完整的URL包括协议,主机名,端口号和路径。- 通过在 URL 上调用
openConnection
方法创建连接对象。 - 处理设置参数和一般请求属性。
- 使用
connect
方法建立到远程对象的实际连接。 - 远程对象变为可用。远程对象的头字段和内容变为可访问。
以前也在学网络编程的时候也尝试写过UDP的聊天程序,不过老师讲的是用c++,我是用java写的。但是最后全班除了我们组是自己写的外,其他组都是从网上down的。因为老师不反对,前提是别人的程序要读懂。可惜老师并不怎么喜欢java,给了我们组比较低的分。
程序比较简单,没有太复杂的实现,只是简单的实现了聊天功能。但是程序每次启动的时候必须指定一个监听的端口,且之后不能修改监听的端口号。同时还需指定目的端口号,可以在程序运行的过程中修改。关于目的ip地址保存在一个文件中,在界面上显示是通过JComboBox选择的,可以进行添加和删除。同时接收窗口和发送窗口用多线程实现,接收窗口带滚动条,比较容易混淆的一点是,将JTextArea当作参数传给JScrollPanel的构造函数,之后只需将JScrollPanel添加进容器中就可以了,数据仍然添加进JTestArea中。关于滚动条的位置研究了很长时间,还是没弄太清楚。比如在接收框中需要将滚动条一直置在最底部,需将滚动条的值设为最大值setValue(int value),而将滚动条的getMaximum()返回的值带入后滚动条并不是在最底端,奇怪的是当调用两次设置最大值函数后就可以了。
jsb.setValue(jsb.getMaximum());
jsb.setValue(jsb.getMaximum());
不过用google搜到另一个方法可行,将光标放在接收框的末尾,然后调用一次setValue()函数。
JScroll.java
import java.awt.FlowLayout;import java.awt.GridLayout;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;import java.util.Calendar;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class JScroll extends JPanel implements Runnable{ JPanel subPanel = new JPanel(); JLabel lisLab = null; JTextField listenport = null; //JButton btnLis = null; JTextArea jta = null; JScrollBar jsb = null; JScrollPane jsp = null; DatagramSocket recvSocket = null; DatagramPacket recvPacket = null; public JScroll(){ String s = UdpChat.portInput.getText(); int ss = 0; try{ ss = Integer.parseInt(s); }catch(NumberFormatException e){ System.out.println("listening port must be a integer!"); ss = 52525; }finally{ if(ss>65535 || ss<1){ s = "52525"; } } subPanel.setLayout(new GridLayout(3,1)); this.setLayout(new FlowLayout(FlowLayout.TRAILING)); lisLab = new JLabel("listen port↓"); listenport = new JTextField(7); listenport.setText(ss+""); listenport.setEditable(false); jta = new JTextArea(12,55); jta.setLineWrap(true); jta.setEditable(false); jsp = new JScrollPane(jta); jsb = jsp.getVerticalScrollBar(); jta.setEditable(false); subPanel.add(lisLab); subPanel.add(listenport); this.add(jsp); this.add(subPanel); } public String formatTime(int t){ String r ="00"; if(t<10){ r = "0"+t; }else{ r = "" + t; } return r; } public void run() { String p = listenport.getText(); int port1 = Integer.parseInt(p); byte buf[] = new byte[1024*8]; try { recvSocket = new DatagramSocket(port1); } catch (SocketException e1) { e1.printStackTrace(); } recvPacket = new DatagramPacket(buf, buf.length); while(true){ try { recvSocket.receive(recvPacket); Calendar cal = Calendar.getInstance(); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); String h,m,s; h = formatTime(hour); m = formatTime(min); s = formatTime(sec); String ip = recvPacket.getAddress().getHostAddress(); int port = recvPacket.getPort(); String msg = new String(buf,0,recvPacket.getLength()); jta.append("msg from ip-【"+ip+":"+port+"】@"+h+":"+m+":"+s+"\r\n"+msg+"\r\n"); jta.setCaretPosition(jta.getDocument().getLength()); jsb.setValue(jsb.getMaximum()); //jsb.setValue(jsb.getMaximum()); msg = ""; } catch (IOException e) { e.printStackTrace(); } } }}
TimeLable.java
import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Calendar;import javax.swing.JLabel;class TimeLabel extends JLabel implements Runnable{ private static final long serialVersionUID = 1L; /** * 覆盖Runnable接口的run()方法,此为该线程要运行的代码<br/> * */ public void run() { /**/Calendar cal = Calendar.getInstance(); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int msec = 0; while(true){ String h,m,s,ms; h = hour+""; m = min+""; s = sec+""; ms = msec+""; if(msec<10){ ms = "0"+msec; } if(sec<10){ s = "0"+sec; } if(min<10){ m = "0"+min; } this.setHorizontalAlignment(RIGHT); try { this.setText(InetAddress.getLocalHost().getHostAddress()+"//"+"系统时间-"+h+":"+m+":"+s+":"+ms); } catch (UnknownHostException e1) { e1.printStackTrace(); } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } if(msec==99){ if(sec==59){ if(min==59){ hour++; min=0; sec=0; msec=0; } else{ min++; sec=0; msec=0; } } else{ sec++; msec=0; } } else{ msec++; } } }}
UdpChat.java
import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import java.util.regex.Pattern;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class UdpChat { JDialog jd = new JDialog(new JFrame(),"PORT",true); public static JTextField portInput = null; public static JTextField portInput2 = null; JLabel labPort = null; JLabel labPort2 = null; JButton btnOk = null; JButton btnNo = null; public UdpChat(){ jd.setLayout(new GridLayout(3,2)); labPort = new JLabel("listenning port:"); labPort2 = new JLabel("destination port:"); portInput = new JTextField(6); portInput2 = new JTextField(6); btnOk = new JButton("OK"); btnNo = new JButton("NO"); jd.add(labPort); jd.add(portInput); jd.add(labPort2); jd.add(portInput2); jd.add(btnOk); jd.add(btnNo); startEvents(); jd.setBounds(520, 250, 300, 150); jd.setVisible(true); } public void startEvents(){ btnNo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jd.setVisible(false); new UdpSend(); } }); } public static void main(String[] args) { new UdpChat(); }}class UdpSend extends JFrame{ Container cont = this.getContentPane(); JPanel jpanel = new JPanel(); JLabel ipLabel = null; JTextField ipInput = null; JButton btnAdd = null; JLabel portLabel = null; JTextField portField = null; JLabel ipsLabel = null; JComboBox ipBox = null; JButton btnDel = null; JButton btnClearAll = null; JPanel msgPanel = new JPanel(); JScroll recv = null; JTextArea send = null; JScrollPane jsp = null; JScrollBar jsb = null; JPanel btnPanel = new JPanel(); TimeLabel time = null; JLabel labNull = null; JButton btnSend = null; JButton btnClose = null; DatagramSocket sendSocket = null; DatagramPacket sendPacket = null; public UdpSend(){ cont.setLayout(new BorderLayout()); jpanel.setLayout(new FlowLayout()); msgPanel.setLayout(new FlowLayout()); btnPanel.setLayout(new FlowLayout()); ipLabel = new JLabel("des ip:"); ipInput = new JTextField(12); btnAdd = new JButton("add"); portLabel = new JLabel("des port:"); portField = new JTextField(5); String s = UdpChat.portInput2.getText(); int ss = 0; try{ ss = Integer.parseInt(s); }catch(NumberFormatException e){ System.out.println("destination port must be a integer!"); ss = 52525; }finally{ if(ss>65535 || ss<1){ s = "52525"; } } portField.setText(ss+""); ipsLabel = new JLabel("ip sets:"); ipBox = new JComboBox(readIp()); ipBox.setEditable(false); btnDel = new JButton("delete"); btnClearAll = new JButton("clear all"); jpanel.add(ipLabel); jpanel.add(ipInput); jpanel.add(btnAdd); jpanel.add(portLabel); jpanel.add(portField); jpanel.add(ipsLabel); jpanel.add(ipBox); jpanel.add(btnDel); jpanel.add(btnClearAll); recv = new JScroll(); msgPanel.add(recv); Thread recvThread = new Thread(recv); recvThread.start(); send = new JTextArea(5,63); send.setLineWrap(true); jsp = new JScrollPane(send); jsb = jsp.getVerticalScrollBar(); msgPanel.add(jsp); time = new TimeLabel(); Thread timeThread = new Thread(time); timeThread.start(); labNull = new JLabel(" "); //文件传输功能赞不可用,未实现!! //filePath = new JTextField(15); //btnTransFile = new JButton("send file"); btnSend = new JButton("send"); btnClose = new JButton("close"); btnPanel.add(time); btnPanel.add(labNull); //文件传输功能赞不可用,未实现!! //btnPanel.add(filePath); //btnPanel.add(btnTransFile); btnPanel.add(btnSend); btnPanel.add(btnClose); this.add(jpanel,BorderLayout.NORTH); this.add(msgPanel,BorderLayout.CENTER); this.add(btnPanel,BorderLayout.SOUTH); events(); this.setBounds(350, 150, 720, 450); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } //事件 public void events(){ btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String inputIp = ipInput.getText(); FileWriter fw = null; try { fw = new FileWriter("ip",true); //System.out.println("hello"); } catch (IOException e1) { e1.printStackTrace(); } if( ipReg(inputIp)){ ipBox.addItem(inputIp); try { fw.write(inputIp+"\r\n"); fw.flush(); } catch (IOException e1) { e1.printStackTrace(); }finally{ if(fw!=null){ try{ fw.close(); }catch(IOException e2){ e2.printStackTrace(); } } } } ipInput.setText(""); } }); btnDel.addActionListener(new ActionListener() { @SuppressWarnings("resource") public void actionPerformed(ActionEvent e) { Object obj = ipBox.getSelectedItem(); ipBox.removeItem(obj); int ipCount = ipBox.getItemCount(); File f = new File("ip"); if(f.exists()){ f.delete(); } BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("ip")); for(int i=0;i<ipCount;i++){ String ip = (String)ipBox.getItemAt(i); bw.write(ip+"\r\n"); bw.flush(); } } catch (IOException e1) { e1.printStackTrace(); }finally{ if(bw!=null){ try{ bw.close(); }catch(IOException e2){ e2.printStackTrace(); } } } } }); btnClearAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ipBox.removeAllItems(); File f = new File("ip"); if(f.exists()){ f.delete(); } } }); btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s = rapString(send.getText()); recv.jta.append(s); sendMsg(); } }); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { closeSocket(); System.exit(0); } }); } @SuppressWarnings("resource") public String[] readIp(){ String[] ips = new String[50]; String[] temp = null; String[] a = {""}; int ipCount = 0; File f = new File("ip"); BufferedReader br = null; if(f.exists()){ try { br = new BufferedReader(new FileReader("ip")); while(true){ try { String ip = br.readLine(); if(ip!="" && ip!=null && ipCount<50){ ips[ipCount]=ip; ipCount++; } if(ip==null){ break; } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ temp = new String[ipCount]; for(int i=0 ;i<ipCount;i++){ temp[i] = ips[i]; } if(br!=null) try{ br.close(); }catch(IOException e){ e.printStackTrace(); } } return temp; }else{ return a; } } public void closeSocket(){ if(sendSocket!=null){ sendSocket.close(); } } public void sendMsg(){ String ip = (String) ipBox.getSelectedItem(); int port = Integer.parseInt(portField.getText()); byte []buf = send.getText().getBytes(); try { sendSocket = new DatagramSocket(); sendPacket = new DatagramPacket(buf, 0, buf.length,InetAddress.getByName(ip),port); try { sendSocket.send(sendPacket); send.setText(""); } catch (IOException e) { e.printStackTrace(); } } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } }public String rapString(String msg){ String head="",foot=""; for(int i=0;i<50;i++){ head = head + "_"; } for(int j=0;j<50;j++){ foot = foot + " ̄"; } msg = head+ "\r\nLOCALHOST:" + msg + "\r\n" +foot + "\r\n"; return msg; }public boolean ipReg(String ip){ boolean flag =Pattern.matches("(\\d{1,3}\\.){3}\\d{1,3}",ip); if(flag){ //split(String regex)用”.“分割,正则表达式里.表示任何字符,需要用\转义,java里\又需要用\来转义,所以此处要两个\。 String s[] = ip.split("\\."); for(int i =0 ;i<s.length;i++){ if(Integer.parseInt(s[i])>255){ flag = false; } } } return flag; }}
TCP和UDP的聊天程序大同小异,不同的是TCP是面向连接的,UDP是面向无连接的,TCP好比打电话,双方确认连接后能通信,UDP好比寄信,只管发送,并不管目的地址是否存在或者能否信件能否送到。TCP编程中若要判断当前连接数,无法通过Socket类对象的isClosed或者isConneced方法来判断,这两个方法只能判断本地socket 的连接状况,不能判断远程的状态。比如客户端断开后,服务器端不能通过调用这两个方法来判断客户端的状态。若socket对象的setKeepAlive(boolean on)置为true,本地主机会隔一段时间(由系统内核参数配置)向远程主机发送一个数据包用于检测连接是否存活。另一种常用的检测连接是否存活的方法是使用心跳包,服务器或客户端每隔一段时间向远程主机发送一段数据,若远程主机给予回应则表示连接存活。(心跳包这一部分正在学习中。。。)
下面通过URL连接来获取指定url上的内容:
public static void main(String[] args) throws IOException { //创建指定的URL地址 URL url = new URL("http://bbs.itheima.com/forum-142-1.html"); //打开连接 URLConnection conn = url.openConnection(); //获得读取流 BufferedInputStream bufin = new BufferedInputStream(conn.getInputStream()); //读取数据 int len = 0; byte buf[] = new byte[1024]; while((len = bufin.read(buf)) != -1){ System.out.println(new String(buf,0,len,"UTF-8")); } }
但是获取到的都是html文档内容,无法解析,如果是在浏览器上就能解析了。
public static void main(String[] args) throws IOException { while(true){ //监听指定端口 ServerSocket server = new ServerSocket(8181); //和客户端建立连接 Socket client = server.accept(); //获取输入流,封装输入流 BufferedReader bufr = new BufferedReader(new InputStreamReader(client.getInputStream())); //获取输出流,封装输出流 BufferedOutputStream buf_out = new BufferedOutputStream(client.getOutputStream()); //将客户端需要访问的地址切割出来 String content = bufr.readLine(); String urlString = content.substring(content.indexOf("/")+1,content.lastIndexOf(" ")); //将地址封装成URL对象 URL url = new URL("http://" + urlString); //打开URL连接 URLConnection con = url.openConnection(); //获取输入流,读取数据 BufferedInputStream bufin = new BufferedInputStream(con.getInputStream()); int len = 0; byte buf[] = new byte[1024]; while((len = bufin.read(buf)) != -1){ //将数据写给浏览器,刷新 buf_out.write(buf, 0, len); buf_out.flush(); } //关闭流 bufin.close(); bufr.close(); buf_out.close(); client.close(); server.close(); } }
- Java Socket基础(一)
- java Socket编程基础
- Java基础---Socket编程
- java中的socket基础
- java 基础之Socket
- java基础--socket
- Java Socket 基础
- Java Socket编程基础
- java Socket编程基础
- java socket编程基础
- Java基础:网络+socket
- JAVA-Socket通信基础
- JAVA Socket基础例程
- java socket 编程基础
- Java基础 — Socket
- Java基础回顾---Socket
- Java Socket 基础编程
- java基础复习-socket
- XCode4.3.3/4.2/4.3 + iOS5.1 无证件开发 、真机调试、生成IPA
- [模拟]subsequence
- 各种坑爹小问题,大麻烦(持续更新)
- opengl 新手上路 ---- 初章(glPushMatrix和glPopMatrix)
- ORA-12154: TNS: 无法解析指定的连接标识符
- java基础--socket
- 2014年影响开发人员加薪的7种编程语言
- Android 获取/设置按键音(Touch Sounds)
- JSP 中js传递和解析URL参数以及中文转码和解码
- 2013还未完成的事
- fileExistsAtPath: isDirectory:用法
- 比起Fedora来Ubuntu的apt(dpkg)包管理就是个垃圾
- SQL SERVER与C#的数据类型对应表
- 在statusBar 后面添加一层视图方法