new socket java_java中如何创建socket连接的过程

展开全部

这是我写过的一个简单聊天软件客62616964757a686964616fe59b9ee7ad9431333332623435户端  你参考下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 jcb = new JComboBox();

private JButton jbsend = new JButton("send");

private JButton jbrefresh = new JButton("refresh");

private InputStream input;

private OutputStream output;

privateSocket 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 

temp[i] = destination.getBytes()[j];

temp[i++] = (byte)message.getBytes().length;

for(int j = 0 ; 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 

{

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值