
高分:求案例代码:java Socket 加请求参数,访问>
import javaawt;
import javaxswing;
import javaawtevent;
import javanet;
import javaio;
public class Client extends JFrame
{
/
/
private static final long serialVersionUID = -4733717749265129757L;
Container con=null;
JTextArea jta = null;
JTextField jtf = null;
//ArrayList al = new ArrayList();
//ServerSocket ss = null;
Socket s = null;
Client(String ip,int port)
{
try
{
s = new Socket(ip,port); //创建一个流套接字并将其连接到指定 IP 地址的指定端口号。
launchFrame();
}
catch(Exception e)
{
eprintStackTrace();
}
new Thread(new ClientR())start();
}
public void sent(String str)
{ //发送消息方法
try
{
DataOutputStream dos = new DataOutputStream(sgetOutputStream()); // 创建一个新的数据输出流,将数据写入指定 返回s的套接字的输出流。
doswriteUTF(str);
}
catch(Exception e)
{
eprintStackTrace();
}
}
public void disconnect() throws Exception
{
sclose(); //失去连接,关闭线程s
}
class ClientR implements Runnable
{//客户端运行
/Runnable 接口应该由那些打算通过某一线程执行其实例的类来实现。类必须定义一个称为 run 的无参数方法。
设计该接口的目的是为希望在活动时执行代码的对象提供一个公共协议。例如,Thread 类实现了 Runnable。
激活的意思是说某个线程已启动并且尚未停止。
此外,Runnable 为非 Thread 子类的类提供了一种激活方式。
通过实例化某个 Thread 实例并将自身作为运行目标,就可以运行实现 Runnable 的类而无需创建 Thread 的子类。
大多数情况下,如果只想重写 run() 方法,而不重写其他 Thread 方法,那么应使用 Runnable 接口。这很重要,
因为除非程序员打算修改或增强类的基本行为,否则不应为该类创建子类。
/
public void run()
{
try
{
DataInputStream dis = new DataInputStream(sgetInputStream());//使用指定的底层 sgetInputStream(s的套接字的输入流) 创建一个 DataInputStream(数据输入流)
String str = disreadUTF();
while(true)
{
jtaappend(str+"\n");
str = disreadUTF();
}
}
catch(Exception e)
{
eprintStackTrace();
}
}
}
public void startClient()
{ //客户端启用
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(Systemin)); //读取数据流
String str = brreadLine();
Systemoutprintln(str);
while(true)
{
sent(str); //发送数据
if(strequals("q"))
{ //如果读取的字符为q
disconnect(); //断开连接
return ;
}
str=brreadLine();
}
}
catch(Exception e)
{
eprintStackTrace();
}
}
public void launchFrame() throws IOException
{ //客户端面板布局
con = thisgetContentPane();
jta = new JTextArea();
jtf = new JTextField();
final JTextField jtf1=new JTextField(10);
final JButton jb=new JButton("确认");
Panel p=new Panel();
//Panel p1=new Panel();
JLabel jl=new JLabel();
jlsetText("your name:");
//jlgetVerticalTextPosition();
jtf1setBackground(ColorPINK);
jtf1setBounds(10, 10, 10, 10);
padd(jl);
padd(jtf1);
padd(jb);
jbaddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//egetWhen();
egetActionCommand();
jtfsetText(jtf1getText()toString()+"say:");
}
});
jtasetBackground(ColorLIGHT_GRAY);
jtasetEditable(false); //不可编辑文本域
JScrollPane jsp = new JScrollPane(jta, //jta上滚动条的创建
JScrollPaneVERTICAL_SCROLLBAR_ALWAYS,
JScrollPaneHORIZONTAL_SCROLLBAR_AS_NEEDED);
//conadd(jb,BorderLayoutWEST);
conadd(jsp,BorderLayoutCENTER);
conadd(p,BorderLayoutNORTH);
conadd(jtf,BorderLayoutSOUTH);
jtfaddActionListener(new ActionListener()
{ ///事件监听
public void actionPerformed(ActionEvent e)
{
String str = jtfgetText()toString();
Clientthissent(str);
jtfsetText(jtf1getText()toString()+"say:");
}
});
thissetTitle("聊天客户端,袭风版");
thissetBounds(200,200,300,400);
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thissetBackground(Colorblue);
thissetVisible(true);
}
public static void main (String[] args)
{
Systemoutprintln("===1");
Client cli = new Client("12716811",3456); //创建一个客户端,并将其连接到指定 IP 地址的指定端口号,其端口号与服务器端一致。与服务器建立连接
Systemoutprintln("===2");
clistartClient(); //127001为本机端口
}
}
//
//import javanet;
//import javaio;
//
///
// 发送给服务器端
///
//class Send implements Runnable
//{
// private Socket socket;
// Send(Socket socket)
// {
// thissocket = socket;
// }
// public void run()
// {
// try
// {
// BufferedReader bufr =
// new BufferedReader(new InputStreamReader(Systemin));
//
// String line = null;
// while((line=bufrreadLine())!=null)
// {
// if("886"equals(line))
// {
// socketclose();
// break;
// }
// byte[] bufString = linegetBytes();
//
// //通过socket对象获取socket流中的输出流对象。
// OutputStream out = socketgetOutputStream();
// outwrite(bufString);
//
// InputStream in = socketgetInputStream();
// byte[] buf = new byte[1024];
// int num = inread(buf);
// String str = new String(buf,0,num);
//
// Systemoutprintln("server:"+str);
// }
//
//
// }
// catch (Exception e)
// {
// Systemoutprintln(etoString());
// }
// }
//}
//
///
// 建立服务器
///
//class Rece implements Runnable
//{
// //1,建立服务端的socket服务。并监听一个端口。以获取客户端发来的数据。
// private ServerSocket serverSocket;
// Rece(ServerSocket serverSocket)
// {
// thisserverSocket = serverSocket;
// }
// public void run()
// {
// try
// {
// while(true){
// Systemoutprintln("============4");
// Socket socket = serverSocketaccept();
// Systemoutprintln("============5");
// String ip = socketgetInetAddress()getHostAddress();
// Systemoutprintln("============6");
// Systemoutprintln(ip+"connected");
// Systemoutprintln("============7");
// BufferedReader in = new BufferedReader(new InputStreamReader(socketgetInputStream()));
// Systemoutprintln("============8");
// String line ="";
// Systemoutprintln("============9");
//
// OutputStream out = socketgetOutputStream();
// outwrite("开始"getBytes());
//
// while((line= inreadLine())!=null){
// Systemoutprintln(line);
// }
//
// }
// }
// catch (Exception e)
// {
// Systemoutprintln(etoString());
// }
// }
//}
//
//
//class ChatDemoServer
//{
// public static void main(String[] args) throws Exception
// {
// //1,建立服务端的socket服务。并监听一个端口。以获取客户端发来的数据。
// Systemoutprintln("========1");
// ServerSocket rece = new ServerSocket(8082);
// receclose();
// Systemoutprintln("========2");
// new Thread(new Rece(rece))start();
// Systemoutprintln("========3");
// }
//}
//
//
//class ChatClient
//{
// public static void main(String[] args) throws Exception
// {
// //1,建立服务端的socket服务。并监听一个端口。以获取客户端发来的数据。
// //建立客户端socket服务。并去连接指定的服务端。
// Socket send = new Socket("169254160",10086);
// if(send == null){
// Systemoutprintln("创建Socket失败!");
// return;
// }
// new Thread(new Send(send))start();
// }
//}
import javanet;
import javaio;
import javautil;
//import javaawtevent;
import javaxswing;
import javaawt;
public class Server extends JFrame
{
/
套接字接口可分为三类:公认端口 注册端口 动态和/或私有端口
套接字,简单的说就是通信的两方的一种约定,用套接字中的相关函数来完成通信过程
/
private static final long serialVersionUID = 4130666237241201704L;
Container con = null; //容器
JTextArea jta = null; //文本区
ServerSocket ss = null; //初始化服务器套接字
ArrayList al = new ArrayList();//ArrayList容器
Server(int port)
{ //构造函数
try
{
ss=new ServerSocket(port); // 创建绑定到特定端口的服务器套接字。
}
catch(Exception e)
{
eprintStackTrace();
}
launchFrame();
}
public void severStart()
{ //线程开始
while(true)
{
try{
Socket s = ssaccept(); //侦听并接受到此套接字(port)的连接。
aladd(new ServerR(s));
jtaappend("New Client:"+"\n Ip: "+sgetInetAddress()+":"+sgetPort()+"\n" //得到客户端本地地址,端口和客户端数量
+"Clients count:"+alsize()+"\n");
}
catch(Exception e)
{
eprintStackTrace();
}
}
}
class ServerR implements Runnable
{
Socket s = null; //创建新服务线程
ServerR(Socket s)
{
thiss = s;
new Thread(this)start();//启动新线程
}
public void sent(String str)
{ //发送数据流方法
try
{
DataOutputStream dos = new DataOutputStream(sgetOutputStream()); //从s套接字中读出输出数据流
doswriteUTF(str); //使用 UTF-8 修改版编码将一个字符串写入基础输出流。
}
catch(Exception e)
{
eprintStackTrace();
}
}
public void run()
{
try
{
DataInputStream dis = new DataInputStream(sgetInputStream());
String str = disreadUTF(); //读出输入的数据流
while(true)
{
// Systemoutprintln (str);
Iterator ite = aliterator(); //生成list迭代器
while(itehasNext())
{ //如果仍有可迭代的元素,返回true
((ServerR)itenext())sent(str); //返回(ServerR)的下一个元素,并发送它
//先遍历ServerR中的所有线程
}
str=disreadUTF();
}
}
catch(Exception e)
{
try{ //客户端关闭捕捉
sclose(); //关闭一个相关客户端线程
alremove(this); //从迭代器指向的集合中移除迭代器返回的最后一个元素
jtaappend("A client quit!\nClients count:"+alsize()+"\n"); //统计客户端现有的数量
}
catch(Exception e2)
{
e2printStackTrace();
}
eprintStackTrace();
}
}
}
public void launchFrame()
{ //服务器端面板布局
con = thisgetContentPane();
jta = new JTextArea();
jtasetEditable(false); //不可编辑文本域
JScrollPane jsp = new JScrollPane(jta,
JScrollPaneVERTICAL_SCROLLBAR_ALWAYS,
JScrollPaneHORIZONTAL_SCROLLBAR_AS_NEEDED);
conadd(jsp,BorderLayoutCENTER);
thissetTitle("聊天服务端,袭风版");
thissetBounds(200,200,300,400);
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thissetVisible(true);
}
public static void main (String[] args)
{
Server s = new Server(3456); //创建服务器S 端口为3456
sseverStart();
}
}
以上就是关于一篇搞懂tcp,http,socket,socket连接池之间的关系全部的内容,包括:一篇搞懂tcp,http,socket,socket连接池之间的关系、为啥需要http转socket,不直接用http、socket编程 ~~急~~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)