Socket Programming In Dev C++

Socket programming in javaSocket
Dev-C++ is a free IDE for Windows that uses either MinGW or TDM-GCC as underlying compiler.
Originally released by Bloodshed Software, but abandoned in 2006, it has recently been forked by Orwell, including a choice of more recent compilers. It can be downloaded from:
http://orwelldevcpp.blogspot.com

Installation

Run the downloaded executable file, and follow its instructions. The default options are fine.

Support for C++11

By default, support for the most recent version of C++ is not enabled. It shall be explicitly enabled by going to:
Tools -> Compiler Options
Here, select the 'Settings' tab, and within it, the 'Code Generation' tab. There, in 'Language standard (-std)' select 'ISO C++ 11':
Ok that. You are now ready to compile C++11!

Compiling console applications

To compile and run simple console applications such as those used as examples in these tutorials it is enough with opening the file with Dev-C++ and hit F11.
As an example, try:
File -> New -> Source File (or Ctrl+N)
There, write the following:
Then:
File -> Save As... (or Ctrl+Alt+S)
And save it with some file name with a .cpp extension, such as example.cpp.
Now, hitting F11 should compile and run the program.
If you get an error on the type of x, the compiler does not understand the new meaning given to auto since C++11. Please, make sure you downloaded the latest version as linked above, and that you enabled the compiler options to compile C++11 as described above.

Tutorial

You are now ready to begin the language tutorial: click here!.
首頁(Home)‎ > ‎研究工具‎ > ‎網路資源‎ > ‎

Scocket Programming with DEV-C++

在 DEV-C++ 中要使用 Windows Socket Programming 必須要連結 Winsock 函式庫。連結函示庫的方法說明如下:
Step 1. 開啟工具列中的「專案」> 「專案選項」
Step 3. 按下「參數」頁面中的「新增函式庫或 Obj 檔」
Step 5. 如果在「連結器」文字框中會出現 ..liblibwsock32.a,則按下「確定」
完成以上步驟後就可以開始撰寫 Windows Socket 程式。

簡單的 server 程式

int main() {
SOCKET server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
// 註冊 Winsock DLL
WSADATA wsadata;
if(WSAStartup(0x101,(LPWSADATA)&wsadata) != 0) {
printf('Winsock Errorn');
server_sockfd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET(使用IPv4); SOCK_STREAM; 0(使用預設通訊協定,即TCP)
if(server_sockfd SOCKET_ERROR) {
printf('Socket Errorn');
// short int sin_family; /* AF_INT(使用IPv4) */
// struct in_addr sin_addr; /* IP位址 */
// sturct in_addr {
// };
server_address.sin_family = AF_INET; // AF_INT(使用IPv4)
server_address.sin_addr.s_addr = inet_addr('127.0.0.1'); // 設定IP位址
server_address.sin_port = 9734; //設定埠號
server_len = sizeof(server_address);
if(bind(server_sockfd, (struct sockaddr *)&server_address, server_len) < 0) {
printf('Bind Errorn');
exit(1);
}

if(listen(server_sockfd, 5) < 0) {
printf('Listen Errorn');
exit(1);
}
while(1) {
char ch;
printf('Server waiting...n');
client_len = sizeof(client_address);

client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
if(client_sockfd SOCKET_ERROR) {
printf('Accept Errorn');
exit(1);
}

recv(client_sockfd, &ch, 1, 0); // Linux socket programming 為 read
ch++;
send(client_sockfd, &ch, 1, 0); // Linux socket programming 為 write
closesocket(client_sockfd); // Linux socket programming 為 close
}
}

簡單的 client 程式

int main() {
SOCKET sockfd;
int len;
struct sockaddr_in address;
int result;
char ch = 'A';
WSADATA wsadata;
if(WSAStartup(0x101,(LPWSADATA)&wsadata) != 0) {
printf('Winsock Errorn');
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr('127.0.0.1');
address.sin_port = 9734;
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result -1) {
printf('Connetc Error');
exit(1);
}
send(sockfd, &ch, 1, 0);
recv(sockfd, &ch, 1, 0);
printf('char from server = %cn', ch);
closesocket(sockfd);
exit(0);
}

Socket Programming In Dev C 2017

Socket

Socket Programming In C

Dec 13, 2017  TCP/IP NETWORK PROGRAMMING IN C ON WINDOWS. A snake game made using WINSOCK LIBRARY. CODEBLOCKS or DEV C COMPILER Used. SOURCE-CODE: http://www.mediafire.