當前位置:首頁 » 操作系統 » socket源碼

socket源碼

發布時間: 2022-01-14 16:48:39

Ⅰ 求一個socket聊天程序源碼 C語言的

通過socket編程自己寫一個唄,可以學習一下socket編程方面的知識

linux中C語言的一個函數的源代碼:socket 編程中的 recv() 函數。

在內核中它是sys_recv()

Ⅲ socket編程問題(附代碼)

1.首先你的程序時是單線程的程序。
2.基於SOCKET的程序,也就是C/S程序,要區分那個是C,那個是S。
3.建議你看下孫鑫的VC++視頻裡面的基於SOCKET通信。

Ⅳ 如何安裝luasocket源碼

第一種方法:如果你有安裝了 Lua 模塊的安裝和部署工具 LuaRocks,那麼一條指令就能安裝部署好 LuaSocket:
# luarocks install luasocket
第二種方法:如果沒安裝有 LuaRocks,也可以源碼安裝。
先把 LuaRocks 下載下來,當前可用的版本是 luasocket-3.0-rc1(luasocket的源碼有託管在Github.com):
# git clone https://github.com/diegonehab/luasocket.git
把源碼clone下來之後就可以進行本地源碼安裝,直接進入到luasocket目錄進行編譯安裝了
# cd luasocket
# make && make install

Ⅳ socket的客戶端與伺服器的代碼

1. 客戶端用Socket對象,請求與伺服器建立連接:
Socket client=new Socket(serverIp,serverPort);

2. 伺服器端用ServerSocket對象,打開伺服器端socket用於監聽請求:
ServerSocket server=new ServerSocket(serverPort);
接收客戶端請求:Socket socket=server.accept();

3.客戶端與伺服器端的互動式通過I/O:inputStream、outputStream進行的

4. 伺服器端最好採用多線程方式響應用戶請求

php100 socket類源碼

email.class.php <?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass; /* Private Variables */ var $sock; /* Constractor */ function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) { $this->debug = FALSE; $this->smtp_port = $smtp_port; $this->relay_host = $relay_host; $this->time_out = 30; //is used in fsockopen() # $this->auth = $auth;//auth $this->user = $user; $this->pass = $pass; # $this->host_name = "localhost"; //is used in HELO command $this->log_file =""; $this->sock = FALSE; } /* Main Function */ function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body); $header .= "MIME-Version:1.0\r\n"; if($mailtype=="HTML"){ $header .= "Content-Type:text/html\r\n"; } $header .= "To: ".$to."\r\n"; if ($cc != "") { $header .= "Cc: ".$cc."\r\n"; } $header .= "From: $from<".$from.">\r\n"; $header .= "Subject: ".$subject."\r\n"; $header .= $additional_headers; $header .= "Date: ".date("r")."\r\n"; $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; list($msec, $sec) = explode(" ", microtime()); $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); } if ($bcc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); } $sent = TRUE; foreach ($TO as $rcpt_to) { $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) { $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); $sent = FALSE; continue; } if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); } else { $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); $sent = FALSE; } fclose($this->sock); $this->log_write("Disconnected from remote host\n"); } echo "<br>"; echo $header; return $sent; } /* Private Functions */ function smtp_send($helo, $from, $to, $header, $body = "") { if (!$this->smtp_putcmd("HELO", $helo)) { return $this->smtp_error("sending HELO command"); } #auth if($this->auth){ if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { return $this->smtp_error("sending HELO command"); } if (!$this->smtp_putcmd("", base64_encode($this->pass))) { return $this->smtp_error("sending HELO command"); } } # if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { return $this->smtp_error("sending MAIL FROM command"); } if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { return $this->smtp_error("sending RCPT TO command"); } if (!$this->smtp_putcmd("DATA")) { return $this->smtp_error("sending DATA command"); } if (!$this->smtp_message($header, $body)) { return $this->smtp_error("sending message"); } if (!$this->smtp_eom()) { return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); } if (!$this->smtp_putcmd("QUIT")) { return $this->smtp_error("sending QUIT command"); } return TRUE; } function smtp_sockopen($address) { if ($this->relay_host == "") { return $this->smtp_sockopen_mx($address); } else { return $this->smtp_sockopen_relay(); } } function smtp_sockopen_relay() { $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); $this->log_write("Error: ".$errstr." (".$errno.")\n"); return FALSE; } $this->log_write("Connected to relay host ".$this->relay_host."\n"); return TRUE;; } function smtp_sockopen_mx($address) { $domain = ereg_replace("^.+@([^@]+)$", "\\1", $address); if (!@getmxrr($domain, $MXHOSTS)) { $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); return FALSE; } foreach ($MXHOSTS as $host) { $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); $this->log_write("Error: ".$errstr." (".$errno.")\n"); continue; } $this->log_write("Connected to mx host ".$host."\n"); return TRUE; } $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); return FALSE; } function smtp_message($header, $body) { fputs($this->sock, $header."\r\n".$body); $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); return TRUE; } function smtp_eom() { fputs($this->sock, "\r\n.\r\n"); $this->smtp_debug(". [EOM]\n"); return $this->smtp_ok(); } function smtp_ok() { $response = str_replace("\r\n", "", fgets($this->sock, 512)); $this->smtp_debug($response."\n"); if (!ereg("^[23]", $response)) { fputs($this->sock, "QUIT\r\n"); fgets($this->sock, 512); $this->log_write("Error: Remote host returned \"".$response."\"\n"); return FALSE; } return TRUE; } function smtp_putcmd($cmd, $arg = "") { if ($arg != "") { if($cmd=="") $cmd = $arg; else $cmd = $cmd." ".$arg; } fputs($this->sock, $cmd."\r\n"); $this->smtp_debug("> ".$cmd."\n"); return $this->smtp_ok(); } function smtp_error($string) { $this->log_write("Error: Error occurred while ".$string.".\n"); return FALSE; } function log_write($message) { $this->smtp_debug($message); if ($this->log_file == "") { return TRUE; } $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); return FALSE; } flock($fp, LOCK_EX); fputs($fp, $message); fclose($fp); return TRUE; } function strip_comment($address) { $comment = "\\([^()]*\\)"; while (ereg($comment, $address)) { $address = ereg_replace($comment, "", $address); } return $address; } function get_address($address) { $address = ereg_replace("([ \t\r\n])+", "", $address); $address = ereg_replace("^.*<(.+)>.*$", "\\1", $address); return $address; } function smtp_debug($message) { if ($this->debug) { echo $message."<br>"; } } function get_attach_type($image_tag) { // $filedata = array(); $img_file_con=fopen($image_tag,"r"); unset($image_data); while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag)))) $image_data.=$tem_buffer; fclose($img_file_con); $filedata['context'] = $image_data; $filedata['filename']= basename($image_tag); $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,".")); switch($extension){ case ".gif": $filedata['type'] = "image/gif"; break; case ".gz": $filedata['type'] = "application/x-gzip"; break; case ".htm": $filedata['type'] = "text/html"; break; case ".html": $filedata['type'] = "text/html"; break; case ".jpg": $filedata['type'] = "image/jpeg"; break; case ".tar": $filedata['type'] = "application/x-tar"; break; case ".txt": $filedata['type'] = "text/plain"; break; case ".zip": $filedata['type'] = "application/zip"; break; default: $filedata['type'] = "application/octet-stream"; break; } return $filedata; } } ?> ----------------------------------------- sendmail.php <?php require_once ('email.class.php'); //########################################## $smtpserver = "smtp.163.com";//SMTP伺服器 $smtpserverport =25;//SMTP伺服器埠 $smtpusermail = "";//SMTP伺服器的用戶郵箱 $smtpemailto = "";//發送給誰 $smtpuser = "";//SMTP伺服器的用戶帳號 $smtppass = "";//SMTP伺服器的用戶密碼 $mailsubject = "PHP100測試郵件系統";//郵件主題 $mailbody = "<h1> 這是一個測試程序 PHP100.com </h1>";//郵件內容 $mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件 ########################################## $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證. $smtp->debug = FALSE;//是否顯示發送的調試信息 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); ?>

Ⅶ linux socket網路編程學完看哪些源碼

要看的東西實在是太多了,不如來培訓一下吧。

Ⅷ linux下socket 網路編程(客戶端向伺服器端發送文件) 求源代碼 大哥大姐幫幫忙 ,。。謝謝

不知道是樓下還是樓上,他用的事windows的套接字貌似,不過過程是一樣的,在Linux下面使用的函數名字不同罷了,客戶端向伺服器端發送文件,過程其實很簡單:
1.客戶端向伺服器端發送傳輸文件的請求(可攜帶一些所傳文件的大小之類的基本信息);
2.伺服器端接收請求(根據文件信息看是否能夠接受請求);
3.客戶端發送文件(如果接收到server的yes響應,那麼就傳送);
4.伺服器端接收文件(寫入到指定目錄的文件夾);

你需要先簡單了解過程,在弄清除Linux下面對socket的操作(其實就類似於對文件的操作)。

如果實在自己寫不出可以:email:[email protected]

Ⅸ 如何運行socket 代碼

呵呵 這個工具剛剛用過 SocketTool
這個是windows下面的,可以裝在windows下面,分別測試你linux的客戶端和服務端
有郵箱的話 我發給你

Ⅹ 使用c++6.0,基於Socket開發網路音頻點播程序。求源碼!

「對圖中的那些函數,我這里稍加解釋一下。」 int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData); 功能是初始化Windows Socket Dll,在Windows下必須使用它。參數:「wVersionRequested」表示版本,可以是1.1、2.2等;「lpWSAData」指向WSADATA數據結構的指針。int socket(int family, int type, int protocol); 功能是建立Socket,返回以後會用到的Socket值。如果錯誤,返回-1。參數:「int family」參數指定所要使用的通信協議,取以下幾個值:AF_UNIX(Unix內部協議)、AF_INET(Internet協議)、AF_NS Xerox(NS協議)、AF_IMPLINK(IMP連接層),在Windows下只能把「AF」設為「AF_INET」;「int type」參數指定套接字的類型,取以下幾個值:SOCK_STREAM(流套接字)、SOCK_DGRAM (數據報套接字)、SOCK_RAW(未加工套接字)、SOCK_SEQPACKET(順序包套接字);「int protocol」參數通常設置為0。int bind(int sockfd, struct sockaddr *my_addr, int addrlen); 功能是把套接字和機器上一定的埠關聯起來。參數:「sockfd」是調用socket()返回的套接字值;「my_addr」是指向數據結構struct sockaddr的指針,它保存你的地址,即埠和IP地址信息;「addrlen」設置為sizeof(struct sockaddr)。 int listen(int sockfd, int backlog); 功能是服務端監聽一個埠,直到accept()。在發生錯誤時返回-1。參數:「sockfd」是調用socket()返回的套接字值;「backlog」是允許的連接數目。大多數系統的允許數目是20,也可以設置為5到10。 int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); 功能是客戶端連接服務端監聽的埠。參數:「sockfd」是調用socket()返回的套接字值;「serv_addr」保存著目的地埠和IP 地址的數據結構struct sockaddr;「addrlen」設置為sizeof(struct sockaddr)。 int accept(int sockfd, void *addr, int *addrlen); 功能是服務端接受客戶端的連接請求,並返回一個新的套接字,以後服務端的數據傳輸就使用這個新的套接字。如果有錯誤,返回-1。參數:「sockfd」是和listen()中一樣的套接字值;「addr」是個指向局部的數據結構sockaddr_in的指針;「addrlen」設置為sizeof(struct sockaddr_in)。int send(int sockfd, const void *msg, int len, int flags);int recv(int sockfd, void *buf, int len, unsigned int flags);功能是用於流式套接字或數據報套接字的通訊,我們數據的真正傳輸就由它們完成。參數:「sockfd」是發/收數據的套接字值;「msg」指向你想發送的數據的指針;「buf」是指向接收數據存放的地址;「len」是數據的長度;「flags」設置為 0。int sendto(int sockfd, const void *msg, int len, unsigned int flags,const struct sockaddr *to, int tolen);int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);功能和send、recv類似,不過是用於無連接數據報套接字的傳輸。 int closesocket(int sockfd) 功能是關閉套接字。參數「sockfd」為要關閉的套接字值。程序:「這里的目的是讓大家對Socket編程有個整體了解。不用怕,程序我會詳細解釋的,首先是服務端的程序。其流程是: socket()→bind()→listen→accept()→recv()/send()→closesocket() 具體代碼如下:」★#include #include #pragma comment(lib,"Ws2_32")#define MYPORT 830 /*定義用戶連接埠*/ #define BACKLOG 10 /*多少等待連接控制*/ int main() { int sockfd, new_fd; /*定義套接字*/ struct sockaddr_in my_addr; /*本地地址信息 */ struct sockaddr_in their_addr; /*連接者地址信息*/ int sin_size; WSADATA ws; WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll //建立socket if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { //如果建立socket失敗,退出程序 printf("socket error\n"); exit(1); } //bind本機的MYPORT埠 my_addr.sin_family = AF_INET; /* 協議類型是INET */ my_addr.sin_port = htons(MYPORT); /* 綁定MYPORT埠*/ my_addr.sin_addr.s_addr = INADDR_ANY; /* 本機IP*/ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) { //bind失敗,退出程序 printf("bind error\n"); closesocket(sockfd); exit(1); } //listen,監聽埠 if (listen(sockfd, BACKLOG) == -1) { //listen失敗,退出程序 printf("listen error\n"); closesocket(sockfd); exit(1); } printf("listen"); //等待客戶端連接 sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { printf("accept error\n"); closesocket(sockfd); exit(1); } printf("\naccept!\n"); //有連接,發送ww0830字元串過去 if (send(new_fd, "ww0830\n", 14, 0) == -1) { printf("send error"); closesocket(sockfd); closesocket(new_fd); exit(1); } printf("send ok!\n"); //成功,關閉套接字 closesocket(sockfd); closesocket(new_fd); return 0;}對服務端程序的流程概括:先是初始化Windows Socket Dll: WSAStartup(MAKEWORD(2,2),&ws); 然後建立Socket: sockfd = socket(AF_INET, SOCK_STREAM, 0) 再bind本機的MYPORT埠:my_addr.sin_family = AF_INET; /* 協議類型是INET */ my_addr.sin_port = htons(MYPORT); /* 綁定MYPORT埠 */ my_addr.sin_addr.s_addr = INADDR_ANY; /* 本機IP */ bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) 接下來監聽埠: listen(sockfd, BACKLOG) 如果有客戶端的連接請求,接收它: new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size) 最後發送ww0830字元串過去: send(new_fd, "ww0830\n", 14, 0) 收尾工作,關閉socket: closesocket(sockfd); closesocket(new_fd); 」編譯、執行,就會一直監聽830埠客戶端程序了。其流程是: socket()→connect()→send()/recv()→closesocket() 比服務端更簡單吧!其實現代碼如下:」★#include #include #include #pragma comment(lib,"Ws2_32") #define PORT 830 /* 客戶機連接遠程主機的埠 */ #define MAXDATASIZE 100 /* 每次可以接收的最大位元組 */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct sockaddr_in their_addr; /* 對方的地址埠信息 */ if (argc != 2) { //需要有服務端ip參數 fprintf(stderr,"usage: client hostname\n"); exit(1); } WSADATA ws;WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //如果建立socket失敗,退出程序 printf("socket error\n"); exit(1); } //連接對方their_addr.sin_family = AF_INET; /* 協議類型是INET */ their_addr.sin_port = htons(PORT); /* 連接對方PORT埠 */ their_addr.sin_addr.s_addr = inet_addr(argv[1]); /* 連接對方的IP */ if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){ //如果連接失敗,退出程序 printf("connet error\n"); closesocket(sockfd); exit(1); } //接收數據,並列印出來if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { //接收數據失敗,退出程序 printf("recv error\n"); closesocket(sockfd); exit(1); } buf[numbytes] = '\0'; printf("Received: %s",buf); closesocket(sockfd); return 0; } 對客戶端程序的流程概括:首先是初始化Windows Socket Dll: WSAStartup(MAKEWORD(2,2),&ws); 然後建立Socket: sockfd = socket(AF_INET, SOCK_STREAM, 0) 接著連接伺服器方:their_addr.sin_family = AF_INET; /* 協議類型是INET */ their_addr.sin_port = htons(PORT); /* 連接對方PORT埠 */ their_addr.sin_addr.s_addr = inet_addr(argv[1]); /* 連接對方的IP */ connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) 連接成功就接收數據: recv(sockfd, buf, MAXDATASIZE, 0) 最後把收到的數據列印出來並關閉套接字: printf("Received: %s",buf); closesocket(sockfd); 編譯結束後,運行服務端,然後。客戶端 服務端IP 回車你會看到服務端發來得數據。那麼基本的點對點通信就沒問題了。只要兩台機器同時包含服務端和客戶端,就可以互相通信了。當然,你也可以將服務端和客戶端分開做,專門一個伺服器負責用戶登錄和轉發消息。流程如下:A客戶端發登錄消息-----》伺服器伺服器驗證發送用戶消息----》客戶端A客戶端想發消息給B客戶端----》先發給服務端伺服器得到消息查詢B客戶端IP並轉發消息。(或者B客戶端循環發消息詢問伺服器有無消息)通信結束。

熱點內容
壓縮某個文件夾 發布:2024-11-15 09:03:11 瀏覽:891
網址能解壓嗎 發布:2024-11-15 08:54:09 瀏覽:933
python更改目錄 發布:2024-11-15 08:41:08 瀏覽:265
伺服器快閃記憶體可以裝在一般電腦上嗎 發布:2024-11-15 08:36:46 瀏覽:8
安卓手機怎麼查詢自己的路線軌跡 發布:2024-11-15 08:32:19 瀏覽:969
phpdatet 發布:2024-11-15 08:32:17 瀏覽:507
HDB3編解碼實驗 發布:2024-11-15 08:17:31 瀏覽:212
怪星球編程 發布:2024-11-15 08:15:55 瀏覽:844
慧編程價格 發布:2024-11-15 08:14:09 瀏覽:459
python多行注釋的快捷鍵 發布:2024-11-15 08:09:14 瀏覽:957