服务器:
import socket
host = ""
port = 4242
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
client.send(data)
print(data.decode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
客户:
import socket
import sys
host = sys.argv[1]
port = 4242
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
while True:
line = input("What to say: ")
s.send(line.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
好吧,我在这里有点困惑.我开始学习套接字了,所以我开始使用一个简单的echo服务器.当服务器在Arch Linux或Ubuntu上运行时,我上面发布的代码可以很好地工作.但是,当它在Windows 7上时,它只接受本地连接.问题是,我没有运行防火墙.我不确定Python是否有单独的WinSock实现或什么,但我很困惑!如果你愿意的话,我非常清楚这是非常设计的(只接受客户端!),但我只是想知道为什么Windows不接受远程连接.
如果它有帮助,在Arch和Ubuntu上,我正在运行Python 3.1,而在Win 7上它运行在3.2.
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char * cp = malloc(sizeof * cp * 20);
cp="Hello\0";
free(cp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
*** glibc detected *** ./mallocTest: free(): invalid pointer: 0x000000000040069c ***
======= Backtrace: =========
/lib/libc.so.6(+0x71496)[0x7f92ee448496]
/lib/libc.so.6(cfree+0x6c)[0x7f92ee44d29c]
./mallocTest[0x40059e]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f92ee3f5c3d]
./mallocTest[0x4004b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:08 3162120 /home/gabriel/Programming/C/mallocTest
00600000-00601000 rw-p 00000000 08:08 3162120 /home/gabriel/Programming/C/mallocTest
01327000-01348000 rw-p 00000000 00:00 0 [heap]
7f92e8000000-7f92e8021000 rw-p 00000000 00:00 0
7f92e8021000-7f92ec000000 ---p 00000000 00:00 0
7f92ee3d7000-7f92ee52a000 r-xp 00000000 08:08 2452227 /lib/libc-2.12.2.so
7f92ee52a000-7f92ee729000 …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
typedef struct pduct {char name[20];
int price;
int stock;} PRODUCT;
void init(PRODUCT * product)
{
printf("What is the name of the product: ");
fgets(product->name, 20, stdin);
printf("DEBUG: Did it get written...: %s", product->name);
printf("What is the current stock of the item: ");
scanf("%d", product->stock);
printf("What is the price of the new item: ");
scanf("%d", product->price);
}
int main()
{
PRODUCT products[5];
init(products);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我真的有点亏.在运行它时,它会询问产品的名称,将其打印出来,以便我知道它存储了它,然后询问库存量,它将崩溃并返回-1.
我不知道出了什么问题.我试着换出fgets用scanf,只是可以肯定的,但同样的事情发生.我猜我struct的设置错了,但我不知道怎么回事.它char可能是阵列吗?此外,无论我如何安排它,它始终是第二个输入.那么为什么第一个这么好呢?
谢谢你的帮助!