Java编程,利用Socket和ServerSocket编程。

利用Socket编写客户端,利用ServerSocket编写服务端。要求客户端输入一个字符串,服务端能够将该字符串显示出来,并对字符串进行反序重写,将重写结果返回客户端。... 利用Socket编写客户端,利用ServerSocket编写服务端。
要求客户端输入一个字符串,服务端能够将该字符串显示出来,并对字符串进行反序重写,将重写结果返回客户端。
最好是利用Eclipse编写,暂时只看懂这个。
展开
 我来答
censhi007
2009-10-16 · 超过45用户采纳过TA的回答
知道小有建树答主
回答量:176
采纳率:0%
帮助的人:113万
展开全部
你这个不就是TCP的例子吗?我给你找找。
//: c15:JabberServer.java

// Very simple server that just

// echoes whatever the client sends.

// {RunByHand}

import java.io.*;

import java.net.*;

public class JabberServer {

// Choose a port outside of the range 1-1024:

public static final int PORT = 8080;

public static void main(String[] args)

throws IOException {

ServerSocket s = new ServerSocket(PORT);

System.out.println("Started: " + s);

try {

// Blocks until a connection occurs:

Socket socket = s.accept();

try {

System.out.println(

"Connection accepted: "+ socket);

BufferedReader in =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()));

// Output is automatically flushed

// by PrintWriter:

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

while (true) {

String str = in.readLine();

if (str.equals("END")) break;

System.out.println("Echoing: " + str);

out.println(str);

}

// Always close the two sockets...

} finally {

System.out.println("closing...");

socket.close();

}

} finally {

s.close();

}

}

} ///:~

//: c15:JabberClient.java

// Very simple client that just sends

// lines to the server and reads lines

// that the server sends.

// {RunByHand}

import java.net.*;

import java.io.*;

public class JabberClient {

public static void main(String[] args)

throws IOException {

// Passing null to getByName() produces the

// special "Local Loopback" IP address, for

// testing on one machine w/o a network:

InetAddress addr =

InetAddress.getByName(null);

// Alternatively, you can use

// the address or name:

// InetAddress addr =

// InetAddress.getByName("127.0.0.1");

// InetAddress addr =

// InetAddress.getByName("localhost");

System.out.println("addr = " + addr);

Socket socket =

new Socket(addr, JabberServer.PORT);

// Guard everything in a try-finally to make

// sure that the socket is closed:

try {

System.out.println("socket = " + socket);

BufferedReader in =

new BufferedReader(

new InputStreamReader(

socket.getInputStream()));

// Output is automatically flushed

// by PrintWriter:

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

for(int i = 0; i < 10; i ++) {

out.println("howdy " + i);

String str = in.readLine();

System.out.println(str);

}

out.println("END");

} finally {

System.out.println("closing...");

socket.close();

}

}

} ///:~
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lovepetrel
2009-10-16 · TA获得超过1531个赞
知道小有建树答主
回答量:857
采纳率:100%
帮助的人:0
展开全部
哎。。。加分都没有。。。

算了吧。。

服务器端:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

class wangluo1 extends JFrame implements ActionListener {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JTextArea text1 = new JTextArea(30, 30);
//JTextArea text2 = new JTextArea(3, 25);
JButton sent = new JButton("转换");
InputStream in;
OutputStream out;
Socket client;
ServerSocket server;
String str;
String newStr = "";

public wangluo1() {
add("South", panel2);
add(panel1);
panel1.add(text1);
panel2.setLayout(new FlowLayout());
//panel2.add(text2);
panel2.add(sent);
sent.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// try {
// in.close();
// out.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
System.exit(0);
}
});
pack();
setVisible(true);
this.setTitle("服务器:");
try {
server = new ServerSocket(10000);
client = server.accept();
in = client.getInputStream();
out = client.getOutputStream();
text1.append("已连接"+"\n");
} catch (IOException ioe) {
}
while (true) {
try {

byte[] buf = new byte[256];
in.read(buf);
str = new String(buf);
text1.append("客户端说:" + "\n" +str.trim());
text1.append("\n");
} catch (IOException e) {
}
}

}

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

public void actionPerformed(ActionEvent e) {

try {

for(int i=str.length()-1;i>=0;i--){
newStr = newStr + str.charAt(i);
}
byte[] buf = newStr.getBytes();
//text2.setText("");
out.write(buf);
out.flush();
//text1.append("服务器说:"+"\n" + s + "\n");
} catch (IOException ie) {
}
}
}

客户端:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class wangluo2 extends JFrame implements ActionListener {
JFrame frame = new JFrame();
Socket client;
OutputStream os;
InputStream is;
String s;
JTextArea text3 = new JTextArea(30, 30);
JTextArea text4 = new JTextArea(3, 25);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton clientsent = new JButton("发送");

public wangluo2() {
add("South", panel2);
add(panel1);
panel1.add(text3);
panel2.setLayout(new FlowLayout());
panel2.add(text4);
panel2.add(clientsent);
pack();
setVisible(true);
this.setLocation(500, 0);
this.setTitle("客户端");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// try {
// is.close();
// os.close();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }

System.exit(0);
}
});
clientsent.addActionListener(this);
try {
client = new Socket("127.0.0.1", 10000);
text3.append("已连接"+"\n");
is = client.getInputStream();
os = client.getOutputStream();
} catch (IOException ioe) {
}

while (true) {
try {
byte[] buf = new byte[256];
is.read(buf);
String str = new String(buf);
text3.append("服务器说:" + "\n" + str.trim());
text3.append("\n");

} catch (IOException e) {
}
}
}

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

}

public void actionPerformed(ActionEvent e) {
try {
String str = text4.getText();
byte[] buf = str.getBytes();
text4.setText("");
os.write(buf);
os.flush();
text3.append("客户端说:"+"\n" + str + "\n");
} catch (Exception ee) {
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式