python socket 聊天室

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#绑定端口
s.bind(("127.0.0.1", 8888))
while True:
    data = s.recvfrom(1024)
    print(str(data[0].decode("gbk")))
    send_data = input("请输入聊天内容")
    if "exit" in send_data:
        break
    s.sendto(send_data.encode("utf-8"), ('127.0.0.1', 11111))
    # windows是自动进行解析的
s.close()

简单的python socket聊天室就完成了

其中socket.AF_INET是固定的,好像是socket树,SOCK_DGRAM是udp协议。
这个核心是用bind绑定了一个端口,相当于自己在本地开启了一个8888端口。服务端是11111端口

原文地址:https://www.cnblogs.com/Triangle-security/p/11854024.html

时间: 2024-08-08 19:23:31

python socket 聊天室的相关文章

Web Socket 聊天室

Web sockets test Web Socket 聊天室 按下连接按钮,会通过WebSocket发起一个到聊天浏览器的连接. 服务器地址: 用户名: 连接 发送 来自网上.............

python 实现聊天室

所用模块 asyncore 官方介绍, 源码 英文捉鸡点 这里  源码中可以看到其实本质上就对 select 以及 socket 的进一步封装 简单说明 Python的asyncore模块提供了以异步的方式写入套接字服务的客户端和服务器的基础结构. 主要包括 asyncore.loop(…) - 用于循环监听网络事件.loop()函数负责检测一个字典,字典中保存dispatcher的实例. asyncore.dispatcher类 - 一个底层套接字对象的简单封装.这个类有少数由异步循环调用的,

socket聊天室

1 #服务端 2 from socket import * 3 import json 4 def recvMsg(s): 5 while True: 6 #接收用户的信息 7 data,address = s.recvfrom(1024) 8 data = json.loads(data) 9 print(data,address) 10 11 if data['type'] == 'enter': 12 # 将用户进入聊天室的信息发给其它所有在线用户 13 sendToAll(('>>系统

【Unity3D自学记录】Unity3D网络之Socket聊天室初探

首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace SocketServer { class Program { const int Port = 20000; //设置连接port static void Main(strin

socket聊天室(服务端)(多线程)(TCP)

#include<string.h> #include<signal.h> #include<stdio.h> #include<sys/socket.h> #include<stdlib.h> #include<netdb.h> #include<pthread.h> #include<memory.h> #include<semaphore.h> int Thread_num=0,count=0

Socket聊天室-TcpListener,TcpClient

参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端: 1 #region 属性 2 3 //请求的客户端连接 4 Socket clientsocket; 5 //当前连接集合 6 List<Client> clients; 7 //请求客户端线程 8 Thread clientservice; 9 //服务器监听线程 10 Thread threadListen; 11 //服务器监听连接 12 TcpListener

Python 简单聊天室

#coding=utf-8 from socket import * from threading import Thread import time udpSocket = socket(AF_INET,SOCK_DGRAM) bind = ('',38438) udpSocket.bind(bind) def sendData(ip,port): connectInfo = (ip,port) while True: msg = input("<<<") msg

socket 聊天室实现

server #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #includ

socket 聊天室

服务端: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Thre