Friday, 6 September 2013

Getting error while doing socket programming in titanium

Getting error while doing socket programming in titanium

I done socket programming in phonegap using plugin .Actually I have one
.exc file while run on my other laptop having same network.I already fetch
data in phonegap using socket .Now I am trying in titanium .but get error
on titanium.
here is my code.
Error is TiSocketError alt line number 79 mean in this line
listenSocket.listen();
//FirstView Component Constructor
function SecondView2(data) {
//create object instance, a parasitic subclass of Observable
var dataArray =new Array();
dataArray[0]="naveee";
var wn = Ti.UI.createWindow({
backgroundColor:'red'
});
var self = Ti.UI.createView({
layout:"vertical",
height:300,
top:40
});
var hostname = '198.168.1.109';
var clientSocket = Ti.Network.Socket.createTCP({
host : hostname,
port : 8081,
connected : function(e) {
Ti.API.info('Client socket connected!');
Ti.Stream.pump(e.socket, pumpCallback, 1024, true);
e.socket.write(Ti.createBuffer({
value : 'A message from a connecting socket.'
}));
},
error : function(e) {
Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
}
});
function writeCallback(e) {
Ti.API.info('Successfully wrote to socket.');
}
function pumpCallback(e) {
// Has the remote socket closed its end?
if (e.bytesProcessed < 0) {
Ti.API.info("Closing client socket.");
clientSocket.close();
return;
}
try {
if(e.buffer) {
var received = e.buffer.toString();
Ti.API.info('Received: ' + received);
} else {
Ti.API.error('Error: read callback called with no buffer!');
}
} catch (ex) {
Ti.API.error(ex);
}
}
//Create a socket and listen for incoming connections
var listenSocket = Ti.Network.Socket.createTCP({
host : hostname,
port : 8081,
accepted : function(e) {
// This where you would usually store the newly-connected socket,
e.inbound
// so it can be used for read / write operations elsewhere in the
app.
// In this case, we simply send a message then close the socket.
Ti.API.info("Listening socket <" + e.socket + "> accepted incoming
connection <" + e.inbound + ">");
e.inbound.write(Ti.createBuffer({
value : 'You have been connected to a listening socket.\r\n'
}));
e.inbound.close();
// close the accepted socket
},
error : function(e) {
Ti.API.error("Socket <" + e.socket + "> encountered error when
listening");
Ti.API.error(" error code <" + e.errorCode + ">");
Ti.API.error(" error description <" + e.error + ">");
}
});
// Starts the socket listening for connections, does not accept them
listenSocket.listen();
Ti.API.info("Listening now...");
// Tells socket to accept the next inbound connection.
listenSocket.accepted gets
// called when a connection is accepted via accept()
Ti.API.info("Calling accept.");
listenSocket.accept({
timeout : 10000
});
// Call connect after a short timeout to ensure the listening socket is
ready to go.
Ti.API.info("Setting timer to connect.");
setTimeout(function(e)
{
Ti.API.info("Calling connect on client socket.");
clientSocket.connect();
}, 500);
// Simplest list data items that can be displayed in a list view
var data = [
{ properties: { title: 'Row 1'} },
{ properties: { title: 'Row 2'} },
{ properties: { title: 'Row 3'} }
];
// Add the list data items to a section
var listSection = Titanium.UI.createListSection({items: data});
// Add the list section to a list view
var listView = Titanium.UI.createListView({sections: [listSection]});
var tableData = [ {title: 'Apples'}, {title: 'Bananas'}, {title:
'Carrots'}, {title: 'Potatoes'} ];
var table = Ti.UI.createTableView({
data: tableData
});
listView.addEventListener('itemclick', function(e){
//alert("ppp"+e.i);
add();
var item = listSection.getItemAt(e.itemIndex);
var modal = require('ui/common/Third').modalWin;
var popupWin = new modal();
popupWin.open();
// alert(item.properties.title);
// alert(e.itemIndex);
/* var infoWin = Titanium.UI.createWindow({
layout : 'vertical',
height : 200,
width : Titanium.UI.SIZE,
backgroundColor:'pink',
opacity:0.60
});
// Create a Label.
var name = Ti.UI.createLabel({
text : 'name',
color : 'blue',
font : {fontSize:15},
height :Titanium.UI.SIZE,
width : Titanium.UI.SIZE,
top : 10,
left : 10,
textAlign : 'center'
});
// Add to the parent view.
infoWin.add(name);
// Create a TextField.
var userName = Ti.UI.createTextField({
height : Titanium.UI.SIZE,
top : 10,
width : Titanium.UI.FILL,
hintText : 'Add your user name',
// Android only
keyboardType : Ti.UI.KEYBOARD_DEFAULT,
returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
infoWin.add(userName);
// Create a Button.
var save = Ti.UI.createButton({
title : 'save',
height : 60,
width : 100,
top : 10,
right : 10
});
// Listen for click events.
save.addEventListener('click', function() {
infoWin.close();
});
var cancel = Ti.UI.createButton({
title : 'cancel',
height : 60,
width : 100,
top : 10,
right : 10
});
cancel.addEventListener('click', function() {
infoWin.close();
});
// Add to the parent view.
infoWin.add(save);
infoWin.add(cancel);
infoWin.open({
modal : true
});*/
});
var search = Titanium.UI.createSearchBar({
barColor : 'red',
height : 200,
hintText : 'What\'s It Worth?',
top : 0
});
var tableData = [ {title: 'Apples'}, {title: 'Bananas'}, {title:
'Carrots'}, {title: 'Potatoes'} ];
var autocomplete_table = Titanium.UI.createTableView({
search : search,
scrollable : true,
top : 0,
data : tableData
});
self.add(listView);
//self.add(autocomplete_table);
wn.add(self);
return wn;
}
function add(){
//console.log("adddddd");
/*var emailDialog = Ti.UI.createEmailDialog();
emailDialog.subject = "Hello from Titanium";
emailDialog.toRecipients = ['foo@yahoo.com'];
emailDialog.messageBody = '<b>Appcelerator Titanium Rocks!</b>';
var f = Ti.Filesystem.getFile('a.txt');
emailDialog.addAttachment(f);
emailDialog.open();*/
/*var Settings =
Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'Settings');
Ti.API.info("Created Settings: " +
Settings.createDirectory()+Settings.getNativePath());
Ti.API.info('Settings ' + Settings);
var newFile =
Titanium.Filesystem.getFile(Settings.nativePath,'Settings.txt');
newFile.createFile();
if (newFile.exists()){
newFile.write('line 1\n');
Ti.API.info('newfile: '+newFile.read());
}*/
}
module.exports = SecondView2;

No comments:

Post a Comment