默认的布局 : FlowLayout
Button
button.setLabel("取消");//设置按钮上的文本
// button.setEnabled(b);//设置是否可点击
// String actionCommand = button.getActionCommand();//获取控件的信息
// System.out.println(actionCommand);
Lable
label.setText("1123");//设置文本
label.getText();//获取文本
TextField 文本框
field.setEchoChar(‘*‘); //把输入内容以‘*‘的形式体现
field.setEditable(false); //设置是否可输入
field.setEnabled(false);//设置是否可操作
三个常见布局管理器
1、FlowLayout 流线型布局
frame.setLayout(new FlowLayout());//默认居中对齐 ,左右、上下间隔5
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//设置左对齐 ,左右、上下间隔5
frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,40));//设置左对齐 ,左右、上下间隔5
2、BorderLayout 边界布局
东西南北中
StrinBuffer
StringBuilder
网络编程
网站编程
IP
192.168.1.1
A
1~126.0.0.0
B
128~191.XXX.0.0
C
192~255.xxx.xxx.0
255.255.255.0
TCP协议是一个面向连接的、可靠的协议 TCP(transmission control protocol) 是专门设计用于在不可靠的因特网上提供可靠的、端到端的字节流通信的协议。它是一种面向连接的协议。TCP连接是字节流而非报文流。
TCP 三次握手
对方建立可靠的连接。一个TCP连接必须要经过三次“对话”才能建立起来,其中的过程非常复杂,只简单的描述下这三次对话的简单过程:主机A向主机B发出连接请求数据包:“我想给你发数据,可以吗?”,这是第一次对话;主机B向主机A发送同意连接和要求同步(同步就是两台主机一个在发送,一个在接收,协调工作)的数据包:“可以,你什么时候发?”,这是第二次对话;主机A再发出一个数据包确认主机B的要求同步:“我现在就发,你接着吧!”,这是第三次对话。三次“对话”的目的是使数据包的发送和接收同步,经过三次“对话”之后,主机A才向主机B正式发送数据。
UDP协议是一个不可靠的、无连接协议 UDP(user datagram protocol ) UDP向应用程序提供了一种发送封装的原始IP数据报的方法、并且发送时无需建立连接。是一种不可靠的连接
Socket
package com.xinboedu. www.Test1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer
{
static boolean isAlive = true;
/**
* @param args
*/
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(6666);
while(isAlive)
{
Socket socket = ss.accept();
new ClienThread(socket).start();
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ClienThread extends Thread
{
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public ClienThread(Socket socket)
{
this.socket = socket;
try
{
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
try
{
while(true)
{
String str = dis.readUTF();
System.out.println(str);
dos.writeUTF("Server:收到!!");
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try
{
socket.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.xinboedu. www.Test1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TestClient
{
static String name = "A";
/**
* @param args
*/
public static void main(String[] args)
{
// DataOutputStream dos;
DataInputStream dis;
try
{
Socket socket = new Socket("127.0.0.1", 6666);
// dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
new OutThread(socket).start();
while(true)
{
String readUTF = dis.readUTF();
System.out.println(readUTF);
}
} catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class OutThread extends Thread
{
private DataOutputStream dos;
private Scanner scanner;
private Socket socket;
@SuppressWarnings("resource")
public OutThread( Socket socket)
{
this.socket = socket;
try
{
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
scanner = new Scanner(System.in);
}
@Override
public void run()
{
try
{
while(true)
{
String next = scanner.next();
if("over".equalsIgnoreCase(next))
{
break;
}
dos.writeUTF(TestClient.name + ":" + next);
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try
{
socket.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
UDP
接收方:
package com.xinboedu. www.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestReciver
{
public static void main(String[] args)
{
try
{
DatagramSocket ds = new DatagramSocket(3333);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf , buf.length);
ds.receive(dp);
String string = new String(dp.getData(),0,dp.getLength());
System.out.println(string);
} catch (SocketException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
发送方:
package com.xinboedu. www.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class TestSend
{
public static void main(String[] args)
{
try
{
DatagramSocket ds = new DatagramSocket();
String str = "asdfasdfasdfasdf";
byte[] bytes = str.getBytes();
DatagramPacket dp =
new DatagramPacket( bytes,
bytes.length,
InetAddress.getByName("127.0.0.1"),
3333);
ds.send(dp);
} catch (SocketException e)
{
e.printStackTrace();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
TCP
Socket
可靠 ,长连接
Http
可靠,短链接
一次请求,回应结束,连接断开
URL 统一资源定位符
get post
http://192.168.1.1:8080?ww=1&wq=2
package com.xinboedu. www.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TestHttp
{
/**
* @param args
*/
public static void main(String[] args)
{
String path = " http://localhost:8080/day17.zip";
String str = "E:\\day.zip";
InputStream is = null;
FileOutputStream fos = null;
try
{
URL url = new URL(path);
URLConnection openConnection = url.openConnection();
openConnection.setConnectTimeout(1000);//设置连接超时时间
openConnection.setReadTimeout(1000);//设置读取超时时间
is = openConnection.getInputStream();
fos = new FileOutputStream(str);
byte[] b = new byte[1024];
int len = 0;
while(-1 != (len = is.read(b )))
{
fos.write(b, 0, len);
}
fos.flush();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(is != null)
{
try
{
is.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null)
{
try
{
fos.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
原文地址:http://10114413.blog.51cto.com/10104413/1650778