Java Socket Test

package com.xiaoge;
import java.io.*;


import java.net.*;
public class TCPServer {
public static void main(String args[])throws Exception{
ServerSocket ss = new ServerSocket(6666);
while(true){
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());

System.out.println(dis.readUTF());
dis.close();
s.close();
}
}

}


package com.xiaoge;
import java.net.*;
import java.io.*;
public class TCPClient{
public static void main(String args[]) throws Exception{
Socket s = new Socket("127.0.0.1",6666);

OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
Thread.sleep(3000);
dos.writeUTF("hello server");
dos.flush();
dos.close();
s.close();
}
}



package com.xiaoge;


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


public class Test21 {
public static void main(String args[]){
try{
ServerSocket ss = new ServerSocket(8888);
while(true){
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello,"+s.getInetAddress()+"port#"+s.getPort()+"bye bye");
dos.close();
s.close();
}
}catch(IOException e){
e.printStackTrace();
System.out.println("程序运行出错!");
}
}
}


package com.xiaoge;
import java.io.*;
import java.net.*;


public class Test22 {
public static void main(String args[]){
try{
Socket s = new Socket("127.0.0.1", 8888);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s.close();
}catch(ConnectException connExc){
connExc.printStackTrace();
System.err.println("服务器连接失败");
}catch(IOException e){
e.printStackTrace();

}
}
}


package com.xiaoge;
import java.io.*;
import java.net.*;


public class Test31 {
public static void main(String args[]){
InputStream in = null;
OutputStream out = null;
try{
ServerSocket ss = new ServerSocket(5888);
Socket socket = ss.accept();
in = socket.getInputStream();
out = socket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
String s = null;
if((s = dis.readUTF()) != null){
System.out.println(s);
System.out.println("from:"+socket.getInetAddress());
System.out.println("port:"+socket.getPort());
}
dos.writeUTF("hi,hello");
dis.close();
dos.close();
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}


package com.xiaoge;


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


public class Test32 {
public static void main(String args[]){
InputStream in = null;
OutputStream out = null;

try{
Socket socket = new Socket("localhost", 5888);
in = socket.getInputStream();
out = socket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF("hey");
String s = null;
if((s = dis.readUTF()) != null){
System.out.println(s);
}
dos.close();
dis.close();
socket.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}


package com.xiaoge;
import java.net.*;
import java.io.*;


public class Test41 {
public static void main(String args[])throws Exception{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true){
ds.receive(dp);
//System.out.println(new String(buf, 0, dp.getLength()));
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readLong());
}
}
}


package com.xiaoge;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class Test42 {
public static void main(String args[]){
long n = 10000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
//byte[] buf = (new String("hello").getBytes());
try {
dos.writeLong(n);
byte[] buf = baos.toByteArray();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值