求关于socket通信的小程序

Eric1drifter 2003-09-20 05:08:28
那位兄台有关于socket的小程序,自己写的有注释的最好,不要太短啊,如果帮助很大的话我给50分
寥寥几分不成敬意,请大家不要吝惜源代码啊
...全文
27 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eric1drifter 2003-09-21
  • 打赏
  • 举报
回复
没有仔细看,又丢人了
Eric1drifter 2003-09-21
  • 打赏
  • 举报
回复
newman0708(nch) ( ) 信誉:90 老兄的一点都看不懂:(
ssht968 2003-09-20
  • 打赏
  • 举报
回复
果然好。。
newman0708 2003-09-20
  • 打赏
  • 举报
回复
#####compile.bat
javac *.java -d .

#####runClient.bat
E:\application\j2sdk1.4.0\bin\java.exe -classpath . chatroom.ChatClient

#####runServer.bat
E:\application\j2sdk1.4.0\bin\java.exe -classpath . chatroom.ChatServer

newman0708 2003-09-20
  • 打赏
  • 举报
回复
#####ChatClient.java

package chatroom;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class ChatClient extends Frame implements ActionListener{
Label label1=new Label("聊天");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
ServerSocket server;
Socket client;
protected PrintWriter m_Out;
protected BufferedReader m_In;
String m_szName="";

public ChatClient(String name){
super("客户机");
this.m_szName =name;
setSize(250,250);
panel.add(label1);
panel.add(tf);
tf.addActionListener(this);
add("North",panel);
add("Center",ta);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
ChatClient.this.m_Out .write("Byebye Everyone!");
System.exit(0);}});
show();
this.m_szName=JOptionPane.showInputDialog(null,"Name");//swing,awt
this.setTitle(this.m_szName) ;
try{
client=new Socket(InetAddress.getLocalHost(),5000);
ta.append("已连接的服务器:"+client.getInetAddress().getHostAddress()+"\n\n");
this.m_In=new BufferedReader(new InputStreamReader(client.getInputStream()));
this.m_Out=new PrintWriter(client.getOutputStream());
this.m_Out.println(this.getName());
this.m_Out.flush() ;
}
catch(IOException ioe){}
String line="";
while(true){
try{
line=this.m_In.readLine();
ta.append("服务器说:"+line+"\n");
}catch(IOException e){}
}
}
public void actionPerformed(ActionEvent e){
String str=tf.getText();
this.m_Out.println(str) ;
this.m_Out.flush();
tf.setText("");
ta.append("我说:"+str);
ta.append("\n");
}

public String getName() {
return m_szName;
}

public void setName(String m_szName) {
this.m_szName = m_szName;
}

public static void main(String args[]){
new ChatClient("abs");
}
}
newman0708 2003-09-20
  • 打赏
  • 举报
回复
正好还在,没有删除。


#####ChatServer.java
package chatroom;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;

public class ChatServer extends Frame implements ActionListener{
Label label1=new Label("聊天");
Panel panel=new Panel();
TextField tf=new TextField(10);
TextArea ta=new TextArea();
ServerSocket server;
Socket client;
InputStream in;
OutputStream out;
Vector m_vCustomsOnLine=new Vector();//客户管理

/**
* 构造函数
*/
public ChatServer(){
super("服务器");//标题
setSize(250,250);
panel.add(label1);
panel.add(tf);
tf.addActionListener(this);//监听
add("North",panel);
add("Center",ta);

//退出程序
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});

show();

try{//捕获异常
server=new ServerSocket(5000);//port
while(true){//进行多个客户的接收
client=server.accept();
CustomThread ct=new CustomThread(this,client);//创建服务线程
this.m_vCustomsOnLine.addElement(ct);
ct.start();//启动服务线程
}
}
catch(IOException ioe){}
}

public void actionPerformed(ActionEvent e){
String str=tf.getText();
Enumeration enum=this.m_vCustomsOnLine.elements() ;
while (enum.hasMoreElements()) {
((CustomThread)enum.nextElement()).write2Client(str);//强制转换
}
ta.append("我说:"+str+"\n");
tf.setText("");
}

public void appendMessage(String msg){
this.ta.append(msg);
}

public static void main(String args[]){
new ChatServer();
}
}


/**
*
*一个用户的线程
*/
class CustomThread extends Thread{
protected Socket m_Client;
String m_szLine;//读取一行数据
String m_szCustomName;//用户名字

protected PrintWriter m_Out;
protected BufferedReader m_In;
protected ChatServer m_Server;//服务器

/**
* 构造函数
* @param group 线程组
* @param server 服务器线程
* @param client 客户接口
*/
CustomThread(ChatServer server,Socket client){
this.m_Server=server;
this.m_Client =client;
try{
this.m_In=new BufferedReader(new InputStreamReader(this.m_Client.getInputStream()));
this.m_Out=new PrintWriter(this.m_Client.getOutputStream());
}catch(IOException e){
try{
this.m_Client.close() ;
}catch(IOException e1){
System.err.println("有问题"+e1);
return;
}
}
this.m_szCustomName="";
try {
this.m_szCustomName =this.m_In.readLine();
}
catch (IOException ex) {
System.err.println("Read Client name error!");
}
this.m_Server.appendMessage("已连接的"+this.m_szCustomName+"\n");
}

/**
* 获取用户名字
* @return 用户名字
*/
public String getCustomName(){
return this.m_szCustomName ;
}

/**
* 设置用户名字
* @param szCustomName 用户名字
*/
public void setCustomName(String szCustomName){
this.m_szCustomName=szCustomName ;
}

/**
* 获取客户接口
* @return 接口
*/
public Socket getSocket(){
return this.m_Client;
}

public void run(){
String line="";
try{
while(true){
line=this.m_In.readLine();//读取

this.m_Server .appendMessage(this.m_szCustomName+"说:"+line+"\n") ;
}
}catch(IOException e){
}catch(NullPointerException e){
}
}

public void write2Client(String msg){
this.m_Out.println(msg) ;
this.m_Out.flush() ;
}
}
rufujian 2003-09-20
  • 打赏
  • 举报
回复
import java.net.*;
import java.io.*;

public class Server
{
ServerSocket server;
DataOutputStream output;
Socket socket;
public Server (){
try{
// create a server on port 5000
server=new ServerSocket(4546);
// display interactive informaion
System.out.println("Server created.");
System.out.println("waiting for client to connect on...");
// waiting for client to connect on...
socket = server.accept();
// client connected
System.out.println("client connected.\nShutdown!");
output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("Welcome to Server.Bye!");
output.close();
server.close();
}
catch(SocketException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
catch(IOException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
}

public static void main(String args[]){
Server game=new Server();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧