java中如何创建socket连接的过程 10
3个回答
展开全部
这是我写过的一个简单聊天软件客户端 你参考下
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 JComboBox<String> jcb = new JComboBox<String>();
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 建议使用Jpcap这个封装好的功能包括几乎所有winpcap的。
百度一下,有帮助文档和Demo可以自己看下。需要的话我也可以上传给你
百度一下,有帮助文档和Demo可以自己看下。需要的话我也可以上传给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//创建Socket 客户端对象
Socket s = new Socket("127.0.0.1",6666);
//创建ServerSocket 服务器端对象。。
ServerSocket ss = new ServerSocket(6666);
//监听服务器连接
s = ss.accept();
Socket s = new Socket("127.0.0.1",6666);
//创建ServerSocket 服务器端对象。。
ServerSocket ss = new ServerSocket(6666);
//监听服务器连接
s = ss.accept();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |