客户端Socket发送接收数据的问题

rinusX 2005-12-24 11:17:30
小弟对socket不太熟悉
我现在想通过Socket向服务器发送1个byte的数据并取得服务器送回的结果。
哪位有例子?
谢谢
...全文
372 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
prok 2005-12-26
  • 打赏
  • 举报
回复
发一个byte不就一个byte.
HengHuan 2005-12-26
  • 打赏
  • 举报
回复
录过,帮顶
yuzl32 2005-12-25
  • 打赏
  • 举报
回复
运行接收(UDPrecv.class),然后再运行发送(UDPsend)

F:\>java UDPrecv
你好 朋友!
Hello X From :127.0.0.1 Port :1994

yuzl32 2005-12-25
  • 打赏
  • 举报
回复
// 文件 1
import java.net.*;
import java.io.IOException;

public class UDPrecv {

public static void main(String[] args) {

try {
DatagramSocket ds = new DatagramSocket(3000);

byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);

ds.receive(dp);
System.out.println(new String(dp.getData(),0,dp.getLength()) + " From :"+dp.getAddress().getHostAddress() + " Port :"+
dp.getPort() );

ds.close();
}
catch(SocketException sException){

System.out.println("the socket could not be opened ");
}
catch(IOException ioE){

System.out.println("I/O error occurs ");
}


}
}

// 文件 2
import java.net.*;
import java.io.IOException;

public class UDPsend {

public static void main(String[] args){

try{
DatagramSocket ds = new DatagramSocket();
String data = "你好 朋友!\nHello XiEr !";
DatagramPacket dp = new DatagramPacket(data.getBytes(),data.length(),InetAddress.getByName("127.0.0.1"),3000);

ds.send(dp);
ds.close();
}
catch(SocketException sException){

System.out.println("the socket could not be opened ");
}
catch(IOException ioE){

System.out.println("I/O error occurs ");
}

}
}
rain8050 2005-12-25
  • 打赏
  • 举报
回复
学习学习

如何定义是1个byte??
leeight 2005-12-25
  • 打赏
  • 举报
回复
可能有些方法写错了,自己校正一下吧....
import java.net.*;
public class _class{
public static void main(String args[]){
try{
Socket s = new Socket(ipAddress, port);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
byte i = 10;
os.write(i);// 写入1byte的数据
byte[] rs = new byte[1024];
is.read(rs); // 读取服务器段的数据
s.close();
}catch(Exception e){
}
}
}
PhoticX 2005-12-25
  • 打赏
  • 举报
回复
package untitled10;

import java.net.*;
import java.io.*;

public class temp extends Thread {
private Socket s;
public temp(Socket s) {
this.s = s;
}

// 以下为服务器端程序
public void run() {
try {
OutputStream os = s.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write("Server to client".getBytes());
bos.flush();
//向客户端发送数据流
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
//从客户端读取数据流
bos.close();
is.close();
s.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void server() {
try {
ServerSocket ss = new ServerSocket(6060);
while (true) {
Socket s = ss.accept();
new temp(s).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

//以下为客户端程序
public static void client() {
try {
Socket s = new Socket(InetAddress.getByName(null), 6060);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
os.write("client to server".getBytes());
os.close(); ;
is.close();
s.close();
} catch (Exception ex) {
ex.printStackTrace();
}

}

// 执行时含有参数 启动服务器 否则启动客户端
public static void main(String[] args) {
if (args.length > 0)
server();
else
client();
}


}


wylsx 2005-12-25
  • 打赏
  • 举报
回复
ding
duyhui 2005-12-24
  • 打赏
  • 举报
回复
这样的例子比较多了,楼主网上查查

62,614

社区成员

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

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