linux與windows間socket通訊執行個體

來源:互聯網
上載者:User

linux端:

#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#define MYPORT 3333#define BACKLOG 10main(){ int sockfd, new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {    perror("socket");    exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); bzero(&(my_addr.sin_zero),0); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) {    perror("bind");    exit(1); } if (listen(sockfd, BACKLOG) == -1) {    perror("listen");    exit(1); } while(1) { sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) {    perror("accept");    continue; } printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); if (!fork()) { if (send(new_fd, "Hello, world!\n", 14, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); while(waitpid(-1,NULL,WNOHANG) > 0); }}

window端:

// client.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<Winsock2.h>//#include<Winsock.h>//#include <windows.h> #include<stdio.h>#pragma comment (lib,"WS2_32.lib")#include<stdlib.h>//using namespace std;int _tmain(int argc, _TCHAR* argv[]){ int i; char recvBuffer[255]; WORD wVersionRequested; //typedef unsigned short WORD; 2位元組 WSADATA wsaData; //WSADATA 包含了Windows Socket執行的資訊。 int err; wsaData.wVersion =MAKEWORD(1,1); //這個宏建立一個被指定變數串連而成的WORD變數。返回一個WORD變數。 //第一個是socket庫版本,第二個是取得的版本號碼。 err=WSAStartup(wVersionRequested,&wsaData); //return 0 if successful if(err!=0){  printf("Call WSAStart ERROR!");  exit(1); }   //終止對WinSock庫的使用 //if(LOBYTE(wsaData.wVersion)!=1|| HIBYTE(wsaData.wHighVersion)!=1) //{  //  WSACleanup(); //  exit(0); //} //typedef unsigined int   SOCKET;//建立用與監聽的通訊端 SOCKET SocketClient=socket(AF_INET,SOCK_STREAM,0);       //0表示讓系統自己選擇協議 //定義地址結構體//填入伺服器端的ip地址和連接埠號碼  SOCKADDR_IN addrSrv;  //轉換為TCP/IP network byte order //32bit   addrSrv.sin_addr.S_un.S_addr=inet_addr("172.17.51.81"); //ip 172.17.51.81 addrSrv.sin_family=AF_INET;                              //family address addrSrv.sin_port=htons(3333);                            //16bit連接埠號碼 printf("Connect to server...\n"); i=connect(SocketClient,(sockaddr *)&addrSrv,sizeof(SOCKADDR_IN)); //指向要建立串連的資料結構   if(i<0){  printf("%i\n",WSAGetLastError());  printf("串連到172.17.51.81:3333錯誤!");  exit(1); } recv(SocketClient,recvBuffer,255,0); printf("%s\n",recvBuffer);   //send(socketClient,recvBuffer,20,0); closesocket(SocketClient); WSACleanup();return 0;}

註:

1、windows下控制台程式,需要注意加連結庫ws2_32.lib

2、檔案名稱以cpp結尾,解決SOCKET等符號找不到問題

3、#include<stdio.h> 解決exit符號找不到問題

4、補充第1點:#pragma comment (lib,"WS2_32.lib")

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.