「socket创建java」socket创建对象
本篇文章给大家谈谈socket创建java,以及socket创建对象对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java中的socket是什么意思?
所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄。应用程序通常通过"套接字"向网络发出请求或者应答网络请求。\x0d\x0a以J2SDK-1.3为例,Socket和ServerSocket类库位于java.net包中。ServerSocket用于服务器端,Socket是建立网络连接时使用的。在连接成功时,应用程序两端都会产生一个Socket实例,操作这个实例,完成所需的会话。对于一个网络连接来说,套接字是平等的,并没有差别,不因为在服务器端或在客户端而产生不同级别。不管是Socket还是ServerSocket它们的工作都是通过SocketImpl类及其子类完成的。\x0d\x0a重要的Socket API:\x0d\x0ajava.net.Socket继承于java.lang.Object,有八个构造器,其方法并不多,下面介绍使用最频繁的三个方法,其它方法大家可以见JDK-1.3文档。\x0d\x0a. Accept方法用于产生"阻塞",直到接受到一个连接,并且返回一个客户端的Socket对象实例。"阻塞"是一个术语,它使程序运行暂时"停留"在这个地方,直到一个会话产生,然后程序继续;通常"阻塞"是由循环产生的。\x0d\x0a. getInputStream方法获得网络连接输入,同时返回一个InputStream对象实例。\x0d\x0a. getOutputStream方法连接的另一端将得到输入,同时返回一个OutputStream对象实例。\x0d\x0a注意:其中getInputStream和getOutputStream方法均会产生一个IOException,它必须被捕获,因为它们返回的流对象,通常都会被另一个流对象使用。\x0d\x0a2ServerSocket类例子编辑\x0d\x0a\x0d\x0apackage com.lanber.socket;\x0d\x0aimport java.io.DataInputStream;\x0d\x0aimport java.io.DataOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0aimport java.net.ServerSocket;\x0d\x0aimport java.net.Socket;\x0d\x0apublic class ServerDemo {\x0d\x0a/**\x0d\x0a* 注意:Socket的发送与接收是需要同步进行的,即客户端发送一条信息,服务器必需先接收这条信息,\x0d\x0a* 而后才可以向客户端发送信息,否则将会有运行时出错。\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0aServerSocket ss = null;\x0d\x0atry {\x0d\x0ass = new ServerSocket(8888);\x0d\x0a//服务器接收到客户端的数据后,创建与此客户端对话的Socket\x0d\x0aSocket socket = ss.accept();\x0d\x0a//用于向客户端发送数据的输出流\x0d\x0aDataOutputStream dos = new DataOutputStream(socket.getOutputStream());\x0d\x0a//用于接收客户端发来的数据的输入流\x0d\x0aDataInputStream dis = new DataInputStream(socket.getInputStream());\x0d\x0aSystem.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());\x0d\x0a//服务器向客户端发送连接成功确认信息\x0d\x0ados.writeUTF("接受连接请求,连接成功!");\x0d\x0a//不需要继续使用此连接时,关闭连接\x0d\x0asocket.close();\x0d\x0ass.close();\x0d\x0a} catch (IOException e) {\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a3客户端的例子编辑\x0d\x0apackage com.lanber.socket;\x0d\x0aimportjava.io.DataInputStream;\x0d\x0aimport java.io.DataOutputStream;\x0d\x0aimportjava.io.IOException;\x0d\x0aimport java.io.OutputStream;\x0d\x0aimport java.net.Socket;\x0d\x0aimport java.net.UnknownHostException;\x0d\x0apublic class ClientDemo {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0aSocket socket = null;\x0d\x0atry {\x0d\x0asocket = new Socket("localhost",8888);\x0d\x0a//获取输出流,用于客户端向服务器端发送数据\x0d\x0aDataOutputStream dos = new DataOutputStream(socket.getOutputStream());\x0d\x0a//获取输入流,用于接收服务器端发送来的数据\x0d\x0aDataInputStream dis = new DataInputStream(socket.getInputStream());\x0d\x0a//客户端向服务器端发送数据\x0d\x0ados.writeUTF("我是客户端,请求连接!");\x0d\x0a//打印出从服务器端接收到的数据\x0d\x0aSystem.out.println(dis.readUTF());\x0d\x0a//不需要继续使用此连接时,记得关闭哦\x0d\x0asocket.close();\x0d\x0a} catch (UnknownHostException e) {\x0d\x0ae.printStackTrace();\x0d\x0a} catch (IOException e) {\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
java中如何创建socket连接的过程
这是我写过的一个简单聊天软件客户端 你参考下
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class testChatClient extends JFrame
{
private JTextArea jta = new JTextArea();
private JTextField jtf = new JTextField();
private JComboBoxString jcb = new JComboBoxString();
private JButton jbsend = new JButton("send");
private JButton jbrefresh = new JButton("refresh");
private InputStream input;
private OutputStream output;
private Socket socket;
public static String SERVER_IP = "192.168.1.101";
public static int SERVER_PORT = 8888;
// Message 1 - refresh message
// Message 2 - send message
public testChatClient()
{
initComponents();
try
{
socket = new Socket(SERVER_IP,SERVER_PORT);
input = socket.getInputStream();
output = socket.getOutputStream();
}
catch(IOException e)
{
System.err.println(e);
}
jbrefresh.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setText("");
try
{
if(socket == null)
socket = new Socket(SERVER_IP,SERVER_PORT);
output.write(0x31);
}
catch (IOException ex)
{
JOptionPane.showConfirmDialog(null, ex);
}
}
});
jbsend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf.getText() == null || jtf.getText().equals(""))
return;
if(jtf.getText().length() = 400)
{
JOptionPane.showConfirmDialog(null,"最大字数不能超过400");
return;
}
try
{
String destination = jcb.getSelectedItem().toString();
String message = jtf.getText();
if(socket == null)
socket = new Socket(SERVER_IP,SERVER_PORT);
byte[] temp = new byte[3 + destination.getBytes().length + message.getBytes().length];
temp[0] = 0x32;
temp[1] = (byte)destination.getBytes().length;
int i = 2;
for(int j = 0; j destination.getBytes().length ; i++ , j++)
temp[i] = destination.getBytes()[j];
temp[i++] = (byte)message.getBytes().length;
for(int j = 0 ; j message.getBytes().length ; i++ , j++)
{
temp[i] = message.getBytes()[j];
System.out.println();
}
output.write(temp);
jta.append("me:\n");
jta.append(jtf.getText());
jta.append("\n");
jtf.setText("");
}
catch(IOException ex)
{
System.err.println(ex);
}
}
});
try
{
jbrefresh.doClick();
while(true)
{
byte[] tempBytes = new byte[1000];
input.read(tempBytes);
int command = tempBytes[0] - 0x30;
// int readLength = input.read();
switch(command)
{
case 1:
{
int readLength = tempBytes[1];
String[] temp = new String(tempBytes,2,readLength,"UTF-8").split(";");
jcb.removeAllItems();
if(temp.length == 0 temp[0].equals(""))
return;
for(int i = 0 ; i temp.length ;i++)
{
jcb.addItem(temp[i]);
}
jcb.setSelectedIndex(0);
break;
}
case 2:
{
int readLength1 = tempBytes[1];
jta.append(new String(tempBytes,2,readLength1,"UTF-8") + "\n");
int readLength2 = tempBytes[2 + readLength1];
jta.append(new String(tempBytes,3 + readLength1,readLength2,"UTF-8") + "\n");
break;
}
}
}
}
catch(IOException e)
{
System.err.println(e);
}
}
public static void main(String[] args) {
testChatClient frame = new testChatClient();
}
public void initComponents()
{
setLayout(new BorderLayout());
JPanel jpNorth = new JPanel();
jpNorth.setLayout(new BorderLayout());
jpNorth.add(jcb,BorderLayout.CENTER);
jpNorth.add(jbrefresh,BorderLayout.EAST);
JPanel jpSouth = new JPanel();
jpSouth.setLayout(new BorderLayout());
jpSouth.add(jtf,BorderLayout.CENTER);
jpSouth.add(jbsend,BorderLayout.EAST);
add(jpNorth,BorderLayout.NORTH);
add(jpSouth,BorderLayout.SOUTH);
add(new JScrollPane(jta),BorderLayout.CENTER);
this.getRootPane().setDefaultButton(jbsend);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,600);
setVisible(true);
}
}
java Socket创建的问题
只new一次就行了。
socket是长连接,连接上之后,就一直连着。 除非出错或则你主动断开,才需要重新连接。
socket创建java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于socket创建对象、socket创建java的信息别忘了在本站进行查找喔。