UDP client,UDP server, TCP server, TCP client
UDP server
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException; /**
* udpserver
* 监听端口 10001
* @author GXF
*
*/
public class UdpSocketServer {
private DatagramSocket serverSocket; public UdpSocketServer(int port){
init(port);
} public void init(int port){ InetSocketAddress serverSocketAddress = new InetSocketAddress( port);
try {
serverSocket = new DatagramSocket(serverSocketAddress);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //监听端口
public void startListen(){
byte buff[] = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buff, buff.length);
while(true){
try {
serverSocket.receive(receivePacket);
String message = new String(buff, 0, receivePacket.getLength());
System.out.println("message from client = " + message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String args[]){
UdpSocketServer udpSocketServer = new UdpSocketServer(10001);
udpSocketServer.startListen();
}
}
UDP client
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException; /**
* Udp客户端 DatagramSocket
* @author GXF
*
*/
public class UdpSocketClient {
//客户端套接字
private DatagramSocket clientSocket; //构造方法
public UdpSocketClient(String ip, int port){
init(ip, port);
} /**
* 初始化套接字
* @param ip
* @param port
*/
public void init(String ip, int port){
// try {
// InetAddress serverAddress = InetAddress.getByName(ip);
// InetSocketAddress serverSocketAddress = new InetSocketAddress(serverAddress, port);
try {
clientSocket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// } catch (UnknownHostException | SocketException e) {
// e.printStackTrace();
// }
} /**
* 使用UDP向服务器发送消息
* @param ip
* @param message
*/
public void sendMessage(String message){
byte buff[] = message.getBytes();
DatagramPacket packetToSend;
try {
packetToSend = new DatagramPacket(buff, buff.length, InetAddress.getByName("127.0.0.1"), 10002);
clientSocket.send(packetToSend); //发送数据包
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
String serverIp = "127.0.0.1";
int port = 10001;
UdpSocketClient clientSocket = new UdpSocketClient(serverIp, port);
String message = "hahahahah";
clientSocket.sendMessage(message); } }
TCP server
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket; public class TcpServer {
private ServerSocket serverSocket; TcpServer(int port){
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
} //开始监听端口
public void start(){
while(true){
try {
Socket clientSocket = serverSocket.accept();
InputStream is = clientSocket.getInputStream();
byte buff[] = new byte[1024];
int length = -1;
StringBuilder sb = new StringBuilder();
// while((length = is.read(buff)) > 0){
length = is.read(buff);
sb.append(new String(buff, 0, length));
// }//while
System.out.println("message received: " + sb.toString());
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
} }
} public static void main(String args[]){
int port = 10002;
TcpServer tcpServer = new TcpServer(port);
tcpServer.start();
}
}
Tcp client
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner; public class TcpClient {
private Socket socket;
private int port; private TcpClient(int port){
// try {
this.port = port;
// } catch (IOException e) {
// e.printStackTrace();
// }
} /**
* 发送消息
* @param message
*/
public void sendMessage(String message){
try {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), port);
OutputStream os = socket.getOutputStream();
byte buff[] = message.getBytes();
os.write(buff);
os.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} } public void inputMessage(){
Scanner scanner = new Scanner(System.in);
while(true){
String message = scanner.nextLine();
sendMessage(message);
}
} public static void main(String[] args) {
int port = 10001;
TcpClient client = new TcpClient(port);
client.inputMessage();
} }
UDP client,UDP server, TCP server, TCP client的更多相关文章
- TCP/UDP Socket调试工具提供了TCP Server,TCP Client,UDP Server,UDP Client,UDP Group 五种Socket调试方案。
一.TCP通信测试: 1) 创建TCP Server: 选中左方的TCP Server, 然后点击”创建”按钮,软件弹出监听端口输入框 输入监听端口后,即创建了一个在指定端口上进行监听的TCP S ...
- C# Socket TCP Server & Client & nodejs client
要调试公司某项目里的一个功能,因为要准备测试环境,趁这个机会重温了一下Socket(全还给老师了 -_-#),做个备份. C# Server static void Main(string[] arg ...
- ESP8266开发之旅 网络篇⑦ TCP Server & TCP Client
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select
以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...
- DotNetty 实现 Modbus TCP 系列 (四) Client & Server
本文已收录至:开源 DotNetty 实现的 Modbus TCP/IP 协议 Client public class ModbusClient { public string Ip { get; } ...
- swoole深入学习 2. tcp Server和tcp Client
这节来学习Swoole最基础的Server和Client.会通过创建一个tcp Server来讲解. server <?php class Server { private $serv; pub ...
- TCP server和client的一些测试
一.TCP server和client测试 socket设置 测试项/测试情景 send recv 测 server block client bloc ...
- socket - socketserver - start TCP server
前面提到如何使用socket模块启动tcpserver: 创建socket:sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 绑定ip: ...
- Kcptun 是一个非常简单和快速的,基于KCP 协议的UDP 隧道,它可以将TCP 流转换为KCP+UDP 流
本博客曾经发布了通过 Finalspeed 加速 Shadowsocks 的教程,大家普遍反映能达到一个非常不错的速度.Finalspeed 虽好,就是内存占用稍高,不适合服务器内存本来就小的用户:而 ...
随机推荐
- Python——可变和不可变类型数据
什么是不可变类型? 存储空间保存的数据不允许被修改,这种数据就是不可变类型. 常见的不可变类型有: 数字类型 int, bool, float, complex, long(2.x) 字符串 str ...
- 【数据结构】单链表&&静态链表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 单链表(Singly Linked List ) 1.1 什么是单链表? 单链表是一种链式存储的结构.它动态的为节点分配存 ...
- GCD - Extreme (II) UVA - 11426 数学
Given the value of N , you will have to nd the value of G . The de nition of G is given below: G = i ...
- SQL 判断Null
字段 is null 这是多久没写SQL 了啊....................
- socket长连接 GCDAsyncSocket
基础: http://www.2cto.com/kf/201609/546974.html 转自: http://blog.csdn.net/u013282507/article/details/52 ...
- Java ExecutorService 四种线程池
1.new Thread的弊端 new Thead(new Runnable(){ @Override public void run() { // TODO Auto-generated metho ...
- QT中QWidget、QDialog以及MainWindow的区别
参考 http://blog.csdn.net/u011619422/article/details/47311101 QT中QWidget.QDialog以及MainWindow的区别 QWidge ...
- Windows10 下安装 MySQL Workbench + Thinkphp
昨天,搭建了最基本的 W + I + M + P 环境,今天把 workbench 装上,毕竟效率是第一位的,还不是吾装的时候. MySQL.org 下载最新的 workbench,一路安装倒是没有任 ...
- Modular Inverse (拓展欧几里得求逆元)
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...
- UESTC - 621
f[n]:sigma(i,n),i<n g[n]:sigmaf[i],i<=n #include<bits/stdc++.h> using namespace std; con ...