怎么在同一个主机上实现一个Socket通信呀,谢谢
import java.awt.Color;import java.awt.HeadlessException;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatTCPSocketJFrame extends JFrame implements ActionListener
{
private String name;
private Socket socket;
private JTextArea text_receiver;
private JTextField text_sender;
private JButton button_send,button_leaver;
private PrintWriter cout;
public ChatTCPSocketJFrame(String name,Socket socket) throws IOException
{
super("聊天室"+name+" "+InetAddress.getLocalHost());
this.setSize(300,200);
this.setLocation(200, 200);
this.setBackground(Color.blue);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
text_receiver=new JTextArea();
text_receiver.setEditable(false);
this.getContentPane().add(new JScrollPane(this.text_receiver));
JPanel panel=new JPanel();
this.getContentPane().add(panel);
text_sender=new JTextField(16);
panel.add(text_sender);
button_send=new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
button_leaver=new JButton("离线");
button_leaver.addActionListener(this);
this.setVisible(true);
this.name=name;
this.socket=socket;
this.cout=new PrintWriter(socket.getOutputStream(),true);
this.cout.println(name);
BufferedReader cin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String aline=cin.readLine();
text_receiver.append(aline);
text_receiver.append("连接"+aline+"成功");
aline=cin.readLine();
while(aline!=null && !aline.equals("bye"))
{
text_receiver.append(aline);
aline=cin.readLine();
}
cin.close();
cout.close();
socket.close();
button_send.setEnabled(true);
button_leaver.setEnabled(true);
}
public ChatTCPSocketJFrame(String name, String host,int port)throws IOException
{
this(name,new Socket(host,port));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button_send)
{
this.cout.println(name+"说"+text_sender.getText());
text_receiver.append("我说:"+text_sender.getText());
text_sender.setText("");
}
if(e.getSource()==button_leaver )
{
text_receiver.append("我离线\n ");
this.cout.print(name+"离线\n"+"bye");
button_send.setEnabled( false);
button_leaver.setEnabled(false);
}
}
public static void main(String[] args) throws IOException
{
new ChatTCPSocketJFrame("小蜜蜂","202.119.161.101",2001);
}
}
import java.io.*;
import java.net.*;
public class ChatTCPServer
{ public ChatTCPServer (int port,String name)throws IOException
{ServerSocket server=new ServerSocket(port);
Socket client=server.accept();
new ChatTCPSocketJFrame(name,client);
server.close();
}
public static void main(String args[])throws IOException
{
new ChatTCPServer(2001,"花仙子");
}
}
--------------------编程问答-------------------- 这么长,又不说问题在哪。
同一个主机与不同主机实现socket通信是一样的。同一个主机也需要服务端和客户端。 --------------------编程问答-------------------- 服务端:ServerSocket se = new ServerSocket(5050);
Socket socket = se.accept();
byte[] bytes = new byte[1024];
OutputStream os = new socket.getOutputStream();
InputStream is = new socket.getInputStream();
后面的就是输入输出流的处理了
客户端: Socket socket = new Socket(5050);
OutputStream os = new socket.getOutputStream();
InputStream is = new socket.getInputStream();
后面的就是输入输出流的处理了 --------------------编程问答-------------------- 同一个主机实现socket通信是可以的 只要设一个端口监听就行 然后把消息发送 --------------------编程问答-------------------- 给一个我的结构给你参考吧。
├─base
│ ISteelSocket.java
│ SteelDataPacket.java
│ SteelPacketList.java
│ SteelSocket.java
│
├─conf
│ SteelSocketConfig.java
│
├─error
│ SteelSocketException.java
│
├─net
│ SteelSocketAbstract.java
│ SteelTcpSocket.java
│
├─task
│ SteelSocketInfo.java
│ SteelSocketTask.java
│ SteelTask.java
│
└─tools
SteelSocketCharset.java
SteelSocketCrypto.java
SteelSocketZip.java
============================
代码怎么实现,自己考虑吧。 --------------------编程问答-------------------- 这只是BASE,在这个基础上,再创建一个client和server,因为基础的东西是一样的。 --------------------编程问答-------------------- 有没有错误提示啊,思路和在不同主机上通信是一样的,只是多加个监听 --------------------编程问答-------------------- 呵呵,你怎么维护Session和线程啊 --------------------编程问答--------------------
上面是我的代码,前面是客户端,后面的类服务端,但是我运行不出结果,思路是这样的:首先运行服务端,服务端等待客户端的来申请链接,然后出现两个类似qq桌面。问题是在同一个主机上我不知道怎么处理那个IP地址 --------------------编程问答--------------------
有没有代码呀,代码 --------------------编程问答--------------------
能不能讲的具体点,不是很清楚 --------------------编程问答-------------------- 除
补充:Java , Java SE