小编Mat*_*Mat的帖子

C#获取UDP服务器套接字从中接收数据的客户端的IP地址

我有一个非常奇怪的问题.我无法找到我的服务器从中接收数据的客户端的IP地址.下面是我的UDP Listener类.

IPPacketInformation不包含IP.该clientEndPoint我在我的参考EndReceiveMessageFrom既不.

当我

Console.Writeline(((IPEndPoint)clientEndPoint).Address); 
Run Code Online (Sandbox Code Playgroud)

我得到了服务器的IP地址.我把服务器托管在我自己的机器上,所以我得到了自己的IP地址.当我尝试访问clientEndPoint.remoteEndPoint时,它会抛出一个错误,因为套接字未连接(由于是UDP).

所以基本上,来自外部IP的客户端能够发送数据,但我无法回答客户端,因为我无法检索它的IP.任何帮助表示赞赏!

public class UdpListener
{
    private Socket s;
    public byte[] ReceiveBuffer = new byte[2048];

    public UdpListener()
    {
        s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
        s.Bind(new IPEndPoint(IPAddress.Any, 36200));
    }

    public void Listen()
    {
        try
        {
            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            s.BeginReceiveMessageFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref remoteEndPoint, Recv, s);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }
    }

    private void Recv(IAsyncResult res)
    {
        try
        {
            Socket receiveSocket …
Run Code Online (Sandbox Code Playgroud)

c# udp network-programming

12
推荐指数
1
解决办法
1184
查看次数

Java流组和多个字段相加

我有一个List fooList

class Foo {
    private String category;
    private int amount;
    private int price;

    ... constructor, getters & setters
}
Run Code Online (Sandbox Code Playgroud)

我想按类别分组,然后总和金额以及价格.

结果将存储在地图中:

Map<Foo, List<Foo>> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

关键是Foo持有汇总的金额和价格,列表作为具有相同类别的所有对象的值.

到目前为止,我已经尝试了以下内容:

Map<String, List<Foo>> map = fooList.stream().collect(groupingBy(Foo::getCategory()));
Run Code Online (Sandbox Code Playgroud)

现在我只需要用保存汇总数量和价格的Foo对象替换String键.这是我被困的地方.我似乎无法找到任何办法.

java grouping java-8 java-stream

10
推荐指数
2
解决办法
7436
查看次数

在.htaccess文件中启用SVG文件的压缩和缓存

当我使用Google PageSpeed Insight分析我的网站时,我应该修复一些有关压缩SVG文件的问题.

我一直在网上试图找到解决这个问题的方法,但无论我做什么它似乎都没有用,所以我现在问你们.

我已经检查过gzip是否通过网络上的多个工具启用了,这似乎是真的.

到目前为止,我在.htaccess文件中得到了这个.

RewriteEngine On
Options FollowSymLinks

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /#/$1 [L]

AddType     image/svg+xml              svg svgz
AddEncoding gzip                       svgz

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/html "access 1 day"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access …
Run Code Online (Sandbox Code Playgroud)

compression .htaccess svg caching

4
推荐指数
2
解决办法
1万
查看次数

一旦 int 为零,迭代地从 int 中减去 double (0.5) 就什么都不做。

我有一个循环,每次迭代都double从 an 中减去(0.5) int,它工作得很好,直到int命中 0。为什么值不低于 0?

这是我的代码示例:

int decrease = 2

for(int i = 0; i < 6; i++){
    System.out.println(decrease);
    decrease -= 0.5;
}
Run Code Online (Sandbox Code Playgroud)

输出:

int decrease = 2

for(int i = 0; i < 6; i++){
    System.out.println(decrease);
    decrease -= 0.5;
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我一些替代方案,或者我做错了什么?因为我真的需要一个整数来减少小于 1 并且我知道 anint可以是负数。

java loops

2
推荐指数
1
解决办法
668
查看次数

指定ZonedDateTime的时区而不更改实际日期

我有一个Date对象,它保存一个日期(不是当前日期),我需要以某种方式指定此日期是UTC,然后将其转换为"欧洲/巴黎",即+1小时.

public static LocalDateTime toLocalDateTime(Date date){
    return ZonedDateTime.of(LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC), ZoneId.of("Europe/Paris")).toLocalDateTime();
}
Run Code Online (Sandbox Code Playgroud)

如果日期为"2018-11-08 15:00:00",则将日期转换为"2018-11-08 14:00:00".我需要它从UTC转换到欧洲/巴黎 - 而不是相反.

java timezone date zoneddatetime

2
推荐指数
1
解决办法
1771
查看次数